Bug 136670, bug 132688, add PR_LOG capability. r=jkeiser,sr=darin

This commit is contained in:
bzbarsky%mit.edu 2002-06-17 23:20:46 +00:00
parent 47b2a3cd20
commit c6b1e39242

@ -32,29 +32,46 @@
#include "nsILineInputStream.h"
#include "nsILocalFile.h"
#include "nsIProcess.h"
#include "nsIPref.h"
#include "nsIPrefService.h"
#include "nsIPrefBranch.h"
#include "nsISupportsPrimitives.h"
#include "nsHashtable.h"
#include "nsCRT.h"
#include "prenv.h" // for PR_GetEnv()
#include <stdlib.h> // for system()
nsresult
#ifdef MOZ_LOGGING
#define FORCE_PR_LOG /* Allow logging in the release build */
#endif /* MOZ_LOGGING */
#include "prlog.h"
#ifdef PR_LOGGING
static PRLogModuleInfo *gOSHelperLog = PR_NewLogModule("nsOSHelperAppService");
#endif /* PR_LOGGING */
#define LOG(args) PR_LOG(gOSHelperLog, PR_LOG_DEBUG, args)
#define LOG_ENABLED() PR_LOG_TEST(gOSHelperLog, PR_LOG_DEBUG)
static nsresult
UnescapeCommand(const nsAString& aEscapedCommand,
const nsAString& aMajorType,
const nsAString& aMinorType,
nsHashtable& aTypeOptions,
nsACString& aUnEscapedCommand);
nsresult
static nsresult
FindSemicolon(nsAString::const_iterator& aSemicolon_iter,
const nsAString::const_iterator& aEnd_iter);
nsresult
static nsresult
ParseMIMEType(const nsAString::const_iterator& aStart_iter,
nsAString::const_iterator& aMajorTypeStart,
nsAString::const_iterator& aMajorTypeEnd,
nsAString::const_iterator& aMinorTypeStart,
nsAString::const_iterator& aMinorTypeEnd,
const nsAString::const_iterator& aEnd_iter);
nsresult
static nsresult
GetFileLocation(const char* aPrefName,
const char* aEnvVarName,
PRUnichar** aFileLocation);
static nsresult
LookUpTypeAndDescription(const nsAString& aFileExtension,
nsAString& aMajorType,
nsAString& aMinorType,
@ -63,7 +80,7 @@ LookUpTypeAndDescription(const nsAString& aFileExtension,
inline PRBool
IsNetscapeFormat(const nsAString& aBuffer);
nsresult
static nsresult
CreateInputStream(const nsAString& aFilename,
nsIFileInputStream** aFileInputStream,
nsILineInputStream** aLineInputStream,
@ -71,26 +88,26 @@ CreateInputStream(const nsAString& aFilename,
PRBool* aNetscapeFormat,
PRBool* aMore);
nsresult
static nsresult
GetTypeAndDescriptionFromMimetypesFile(const nsAString& aFilename,
const nsAString& aFileExtension,
nsAString& aMajorType,
nsAString& aMinorType,
nsAString& aDescription);
nsresult
static nsresult
LookUpExtensionsAndDescription(const nsAString& aMajorType,
const nsAString& aMinorType,
nsAString& aFileExtensions,
nsAString& aDescription);
nsresult
static nsresult
GetExtensionsAndDescriptionFromMimetypesFile(const nsAString& aFilename,
const nsAString& aMajorType,
const nsAString& aMinorType,
nsAString& aFileExtensions,
nsAString& aDescription);
nsresult
static nsresult
ParseNetscapeMIMETypesEntry(const nsAString& aEntry,
nsAString::const_iterator& aMajorTypeStart,
nsAString::const_iterator& aMajorTypeEnd,
@ -101,7 +118,7 @@ ParseNetscapeMIMETypesEntry(const nsAString& aEntry,
nsAString::const_iterator& aDescriptionEnd);
nsresult
static nsresult
ParseNormalMIMETypesEntry(const nsAString& aEntry,
nsAString::const_iterator& aMajorTypeStart,
nsAString::const_iterator& aMajorTypeEnd,
@ -111,14 +128,14 @@ ParseNormalMIMETypesEntry(const nsAString& aEntry,
nsAString::const_iterator& aDescriptionStart,
nsAString::const_iterator& aDescriptionEnd);
nsresult
static nsresult
LookUpHandlerAndDescription(const nsAString& aMajorType,
const nsAString& aMinorType,
nsHashtable& aTypeOptions,
nsAString& aHandler,
nsAString& aDescription,
nsAString& aMozillaFlags);
nsresult
static nsresult
GetHandlerAndDescriptionFromMailcapFile(const nsAString& aFilename,
const nsAString& aMajorType,
const nsAString& aMinorType,
@ -136,6 +153,7 @@ nsOSHelperAppService::~nsOSHelperAppService()
NS_IMETHODIMP nsOSHelperAppService::LaunchAppWithTempFile(nsIMIMEInfo * aMIMEInfo, nsIFile * aTempFile)
{
LOG(("-- nsOSHelperAppService::LaunchAppWithTempFile"));
nsresult rv = NS_OK;
if (aMIMEInfo)
{
@ -143,10 +161,17 @@ NS_IMETHODIMP nsOSHelperAppService::LaunchAppWithTempFile(nsIMIMEInfo * aMIMEInf
nsCAutoString path;
aTempFile->GetNativePath(path);
LOG(("Launching helper on '%s'\n", path.get()));
aMIMEInfo->GetPreferredApplicationHandler(getter_AddRefs(application));
if (application)
{
if (LOG_ENABLED()) {
nsCAutoString appPath;
application->GetNativePath(appPath);
LOG(("The helper is '%s'\n", appPath.get()));
}
// if we were given an application to use then use it....otherwise
// make the registry call to launch the app
const char * strPath = path.get();
@ -172,18 +197,22 @@ NS_IMETHODIMP nsOSHelperAppService::LaunchAppWithTempFile(nsIMIMEInfo * aMIMEInf
* Ideally this needs the mime type, mime type options, and location of the
* temporary file, but this last can't be got from here
*/
nsresult
static nsresult
UnescapeCommand(const nsAString& aEscapedCommand,
const nsAString& aMajorType,
const nsAString& aMinorType,
nsHashtable& aTypeOptions,
nsACString& aUnEscapedCommand) {
LOG(("-- UnescapeCommand"));
LOG(("Command to escape: '%s'\n",
NS_LossyConvertUCS2toASCII(aEscapedCommand).get()));
// XXX This function will need to get the mime type and various stuff like that being passed in to work properly
#ifdef DEBUG_bzbarsky
fprintf(stderr, "UnescapeCommand really needs some work -- it should actually do some unescaping\n");
#endif
LOG(("UnescapeCommand really needs some work -- it should actually do some unescaping\n"));
aUnEscapedCommand = NS_ConvertUCS2toUTF8(aEscapedCommand);
LOG(("Escaped command: '%s'\n",
PromiseFlatCString(aUnEscapedCommand).get()));
return NS_OK;
}
@ -191,7 +220,7 @@ UnescapeCommand(const nsAString& aEscapedCommand,
* aStart_iter but before aEnd_iter
*/
nsresult
static nsresult
FindSemicolon(nsAString::const_iterator& aSemicolon_iter,
const nsAString::const_iterator& aEnd_iter) {
PRBool semicolonFound = PR_FALSE;
@ -211,7 +240,7 @@ FindSemicolon(nsAString::const_iterator& aSemicolon_iter,
return NS_OK;
}
nsresult
static nsresult
ParseMIMEType(const nsAString::const_iterator& aStart_iter,
nsAString::const_iterator& aMajorTypeStart,
nsAString::const_iterator& aMajorTypeEnd,
@ -261,42 +290,116 @@ ParseMIMEType(const nsAString::const_iterator& aStart_iter,
return NS_OK;
}
static nsresult
GetFileLocation(const char* aPrefName,
const char* aEnvVarName,
PRUnichar** aFileLocation) {
LOG(("-- GetFileLocation. Pref: '%s' EnvVar: '%s'\n",
aPrefName,
aEnvVarName));
NS_PRECONDITION(aPrefName, "Null pref name passed; don't do that!");
nsresult rv;
*aFileLocation = nsnull;
/* The lookup order is:
1) user pref
2) env var
3) pref
*/
nsCOMPtr<nsIPrefService> prefService(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIPrefBranch> prefBranch;
rv = prefService->GetBranch(nsnull, getter_AddRefs(prefBranch));
NS_ENSURE_SUCCESS(rv, rv);
/*
If we have an env var we should check whether the pref is a user
pref. If we do not, we don't care.
*/
nsCOMPtr<nsISupportsWString> prefFileName;
PRBool isUserPref = PR_FALSE;
prefBranch->PrefHasUserValue(aPrefName, &isUserPref);
if (isUserPref) {
rv = prefBranch->GetComplexValue(aPrefName,
NS_GET_IID(nsISupportsWString),
getter_AddRefs(prefFileName));
if (NS_SUCCEEDED(rv)) {
return prefFileName->GetData(aFileLocation);
}
}
if (aEnvVarName && *aEnvVarName) {
char* prefValue = PR_GetEnv(aEnvVarName);
if (prefValue && *prefValue) {
// the pref is in the system charset and it's a filepath... The
// natural way to do the charset conversion is by just initing
// an nsIFile with the native path and asking it for the Unicode
// version.
nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
NS_ENSURE_SUCCESS(rv, rv);
rv = file->InitWithNativePath(nsDependentCString(prefValue));
NS_ENSURE_SUCCESS(rv, rv);
nsAutoString unicodePath;
rv = file->GetPath(unicodePath);
NS_ENSURE_SUCCESS(rv, rv);
*aFileLocation = ToNewUnicode(unicodePath);
if (!*aFileLocation)
return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
}
rv = prefBranch->GetComplexValue(aPrefName,
NS_GET_IID(nsISupportsWString),
getter_AddRefs(prefFileName));
if (NS_SUCCEEDED(rv)) {
return prefFileName->GetData(aFileLocation);
}
return rv;
}
/* Get the mime.types file names from prefs and look up info in them
based on extension */
nsresult
static nsresult
LookUpTypeAndDescription(const nsAString& aFileExtension,
nsAString& aMajorType,
nsAString& aMinorType,
nsAString& aDescription) {
LOG(("-- LookUpTypeAndDescription for extension '%s'\n",
NS_LossyConvertUCS2toASCII(aFileExtension).get()));
nsresult rv = NS_OK;
nsXPIDLString mimeFileName;
nsCOMPtr<nsIPref> thePrefsService(do_GetService(NS_PREF_CONTRACTID));
if (thePrefsService) {
rv =
thePrefsService->CopyUnicharPref("helpers.private_mime_types_file",
getter_Copies(mimeFileName));
rv = GetFileLocation("helpers.private_mime_types_file",
nsnull,
getter_Copies(mimeFileName));
if (NS_SUCCEEDED(rv) && !mimeFileName.IsEmpty()) {
rv = GetTypeAndDescriptionFromMimetypesFile(mimeFileName,
aFileExtension,
aMajorType,
aMinorType,
aDescription);
} else {
rv = NS_ERROR_NOT_AVAILABLE;
}
if (NS_FAILED(rv) || aMajorType.IsEmpty()) {
rv = GetFileLocation("helpers.global_mime_types_file",
nsnull,
getter_Copies(mimeFileName));
if (NS_SUCCEEDED(rv) && !mimeFileName.IsEmpty()) {
rv = GetTypeAndDescriptionFromMimetypesFile(mimeFileName,
aFileExtension,
aMajorType,
aMinorType,
aDescription);
} else {
rv = NS_ERROR_NOT_AVAILABLE;
}
if (aMajorType.IsEmpty()) {
rv =
thePrefsService->CopyUnicharPref("helpers.global_mime_types_file",
getter_Copies(mimeFileName));
if (NS_SUCCEEDED(rv) && !mimeFileName.IsEmpty()) {
rv = GetTypeAndDescriptionFromMimetypesFile(mimeFileName,
aFileExtension,
aMajorType,
aMinorType,
aDescription);
}
}
} else {
rv = NS_ERROR_NOT_AVAILABLE;
}
return rv;
}
@ -316,13 +419,14 @@ IsNetscapeFormat(const nsAString& aBuffer) {
* Leaves the first line of the file in aBuffer and sets the format to
* PR_TRUE for netscape files and false for normail ones
*/
nsresult
static nsresult
CreateInputStream(const nsAString& aFilename,
nsIFileInputStream ** aFileInputStream,
nsILineInputStream ** aLineInputStream,
nsAString& aBuffer,
PRBool * aNetscapeFormat,
PRBool * aMore) {
LOG(("-- CreateInputStream"));
nsresult rv = NS_OK;
nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
@ -341,13 +445,8 @@ CreateInputStream(const nsAString& aFilename,
nsCOMPtr<nsILineInputStream> lineStream(do_QueryInterface(fileStream, &rv));
#ifdef DEBUG_bzbarsky
if (NS_FAILED(rv)) {
fprintf(stderr, "Interface trouble!\n\n");
}
#endif // DEBUG_bzbarsky
if (NS_FAILED(rv)) {
LOG(("Interface trouble in stream land!"));
return rv;
}
@ -369,12 +468,17 @@ CreateInputStream(const nsAString& aFilename,
/* Open the file, read the first line, decide what type of file it is,
then get info based on extension */
nsresult
static nsresult
GetTypeAndDescriptionFromMimetypesFile(const nsAString& aFilename,
const nsAString& aFileExtension,
nsAString& aMajorType,
nsAString& aMinorType,
nsAString& aDescription) {
LOG(("-- GetTypeAndDescriptionFromMimetypesFile\n"));
LOG(("Getting type and description from types file '%s'\n",
NS_LossyConvertUCS2toASCII(aFilename).get()));
LOG(("Using extension '%s'\n",
NS_LossyConvertUCS2toASCII(aFileExtension).get()));
nsresult rv = NS_OK;
nsCOMPtr<nsIFileInputStream> mimeFile;
nsCOMPtr<nsILineInputStream> mimeTypes;
@ -406,6 +510,8 @@ GetTypeAndDescriptionFromMimetypesFile(const nsAString& aFilename,
entry.Truncate(entry.Length() - 1);
entry.Append(PRUnichar(' ')); // in case there is no trailing whitespace on this line
} else { // we have a full entry
LOG(("Current entry: '%s'\n",
NS_LossyConvertUCS2toASCII(entry).get()));
if (netscapeFormat) {
rv = ParseNetscapeMIMETypesEntry(entry,
majorTypeStart, majorTypeEnd,
@ -413,9 +519,10 @@ GetTypeAndDescriptionFromMimetypesFile(const nsAString& aFilename,
extensions,
descriptionStart, descriptionEnd);
if (NS_FAILED(rv)) {
// We sometimes get things like RealPlayer sticking
// "normal" entries in "Netscape" .mime.types files. Try
// to handle that. Bug 106381
// We sometimes get things like RealPlayer appending
// "normal" entries to "Netscape" .mime.types files. Try
// to handle that. Bug 106381.
LOG(("Bogus entry; trying 'normal' mode\n"));
rv = ParseNormalMIMETypesEntry(entry,
majorTypeStart, majorTypeEnd,
minorTypeStart, minorTypeEnd,
@ -428,13 +535,19 @@ GetTypeAndDescriptionFromMimetypesFile(const nsAString& aFilename,
minorTypeStart, minorTypeEnd,
extensions,
descriptionStart, descriptionEnd);
if (NS_FAILED(rv)) {
// We sometimes get things like StarOffice prepending
// "normal" entries to "Netscape" .mime.types files. Try
// to handle that. Bug 136670.
LOG(("Bogus entry; trying 'Netscape' mode\n"));
rv = ParseNetscapeMIMETypesEntry(entry,
majorTypeStart, majorTypeEnd,
minorTypeStart, minorTypeEnd,
extensions,
descriptionStart, descriptionEnd);
}
}
#ifdef DEBUG_bzbarsky
if (NS_FAILED(rv)) {
fprintf(stderr, "Failed to parse entry: %s\n", NS_ConvertUCS2toUTF8(entry).get());
}
#endif
if (NS_SUCCEEDED(rv)) { // entry parses
nsAString::const_iterator start, end;
extensions.BeginReading(start);
@ -457,12 +570,15 @@ GetTypeAndDescriptionFromMimetypesFile(const nsAString& aFilename,
}
start = iter;
}
} else {
LOG(("Failed to parse entry: %s\n", NS_LossyConvertUCS2toASCII(entry).get()));
}
// truncate the entry for the next iteration
entry.Truncate();
}
}
if (!more) {
rv = NS_ERROR_NOT_AVAILABLE;
break;
}
// read the next line
@ -475,52 +591,61 @@ GetTypeAndDescriptionFromMimetypesFile(const nsAString& aFilename,
/* Get the mime.types file names from prefs and look up info in them
based on mimetype */
nsresult
static nsresult
LookUpExtensionsAndDescription(const nsAString& aMajorType,
const nsAString& aMinorType,
nsAString& aFileExtensions,
nsAString& aDescription) {
LOG(("-- LookUpExtensionsAndDescription for type '%s/%s'\n",
NS_LossyConvertUCS2toASCII(aMajorType).get(),
NS_LossyConvertUCS2toASCII(aMinorType).get()));
nsresult rv = NS_OK;
nsXPIDLString mimeFileName;
nsCOMPtr<nsIPref> thePrefsService(do_GetService(NS_PREF_CONTRACTID));
if (thePrefsService) {
rv =
thePrefsService->CopyUnicharPref("helpers.private_mime_types_file",
getter_Copies(mimeFileName));
rv = GetFileLocation("helpers.private_mime_types_file",
nsnull,
getter_Copies(mimeFileName));
if (NS_SUCCEEDED(rv) && !mimeFileName.IsEmpty()) {
rv = GetExtensionsAndDescriptionFromMimetypesFile(mimeFileName,
aMajorType,
aMinorType,
aFileExtensions,
aDescription);
} else {
rv = NS_ERROR_NOT_AVAILABLE;
}
if (NS_FAILED(rv) || aFileExtensions.IsEmpty()) {
rv = GetFileLocation("helpers.global_mime_types_file",
nsnull,
getter_Copies(mimeFileName));
if (NS_SUCCEEDED(rv) && !mimeFileName.IsEmpty()) {
rv = GetExtensionsAndDescriptionFromMimetypesFile(mimeFileName,
aMajorType,
aMinorType,
aFileExtensions,
aDescription);
} else {
rv = NS_ERROR_NOT_AVAILABLE;
}
if (aFileExtensions.IsEmpty()) {
rv =
thePrefsService->CopyUnicharPref("helpers.global_mime_types_file",
getter_Copies(mimeFileName));
if (NS_SUCCEEDED(rv) && !mimeFileName.IsEmpty()) {
rv = GetExtensionsAndDescriptionFromMimetypesFile(mimeFileName,
aMajorType,
aMinorType,
aFileExtensions,
aDescription);
}
}
} else {
rv = NS_ERROR_NOT_AVAILABLE;
}
return rv;
}
/* Open the file, read the first line, decide what type of file it is,
then get info based on extension */
nsresult
static nsresult
GetExtensionsAndDescriptionFromMimetypesFile(const nsAString& aFilename,
const nsAString& aMajorType,
const nsAString& aMinorType,
nsAString& aFileExtensions,
nsAString& aDescription) {
LOG(("-- GetExtensionsAndDescriptionFromMimetypesFile\n"));
LOG(("Getting extensions and description from types file '%s'\n",
NS_LossyConvertUCS2toASCII(aFilename).get()));
LOG(("Using type '%s/%s'\n",
NS_LossyConvertUCS2toASCII(aMajorType).get(),
NS_LossyConvertUCS2toASCII(aMinorType).get()));
nsresult rv = NS_OK;
nsCOMPtr<nsIFileInputStream> mimeFile;
nsCOMPtr<nsILineInputStream> mimeTypes;
@ -552,6 +677,8 @@ GetExtensionsAndDescriptionFromMimetypesFile(const nsAString& aFilename,
entry.Truncate(entry.Length() - 1);
entry.Append(PRUnichar(' ')); // in case there is no trailing whitespace on this line
} else { // we have a full entry
LOG(("Current entry: '%s'\n",
NS_LossyConvertUCS2toASCII(entry).get()));
if (netscapeFormat) {
rv = ParseNetscapeMIMETypesEntry(entry,
majorTypeStart, majorTypeEnd,
@ -559,6 +686,17 @@ GetExtensionsAndDescriptionFromMimetypesFile(const nsAString& aFilename,
extensions,
descriptionStart, descriptionEnd);
if (NS_FAILED(rv)) {
// We sometimes get things like RealPlayer appending
// "normal" entries to "Netscape" .mime.types files. Try
// to handle that. Bug 106381.
LOG(("Bogus entry; trying 'normal' mode\n"));
rv = ParseNormalMIMETypesEntry(entry,
majorTypeStart, majorTypeEnd,
minorTypeStart, minorTypeEnd,
extensions,
descriptionStart, descriptionEnd);
}
} else {
rv = ParseNormalMIMETypesEntry(entry,
majorTypeStart, majorTypeEnd,
@ -566,13 +704,19 @@ GetExtensionsAndDescriptionFromMimetypesFile(const nsAString& aFilename,
minorTypeEnd, extensions,
descriptionStart, descriptionEnd);
if (NS_FAILED(rv)) {
// We sometimes get things like StarOffice prepending
// "normal" entries to "Netscape" .mime.types files. Try
// to handle that. Bug 136670.
LOG(("Bogus entry; trying 'Netscape' mode\n"));
rv = ParseNetscapeMIMETypesEntry(entry,
majorTypeStart, majorTypeEnd,
minorTypeStart, minorTypeEnd,
extensions,
descriptionStart, descriptionEnd);
}
}
#ifdef DEBUG_bzbarsky
if (NS_FAILED(rv)) {
fprintf(stderr, "Failed to parse entry: %s\n", NS_ConvertUCS2toUTF8(entry).get());
}
#endif
if (NS_SUCCEEDED(rv) &&
Substring(majorTypeStart,
majorTypeEnd).Equals(aMajorType,
@ -585,12 +729,15 @@ GetExtensionsAndDescriptionFromMimetypesFile(const nsAString& aFilename,
aDescription.Assign(Substring(descriptionStart, descriptionEnd));
mimeFile->Close();
return NS_OK;
} else if (NS_FAILED(rv)) {
LOG(("Failed to parse entry: %s\n", NS_LossyConvertUCS2toASCII(entry).get()));
}
entry.Truncate();
}
}
if (!more) {
rv = NS_ERROR_NOT_AVAILABLE;
break;
}
// read the next line
@ -611,7 +758,7 @@ GetExtensionsAndDescriptionFromMimetypesFile(const nsAString& aFilename,
*
* type=foo/bar; options description="Some type" exts="baz"
*/
nsresult
static nsresult
ParseNetscapeMIMETypesEntry(const nsAString& aEntry,
nsAString::const_iterator& aMajorTypeStart,
nsAString::const_iterator& aMajorTypeEnd,
@ -620,6 +767,7 @@ ParseNetscapeMIMETypesEntry(const nsAString& aEntry,
nsAString& aExtensions,
nsAString::const_iterator& aDescriptionStart,
nsAString::const_iterator& aDescriptionEnd) {
LOG(("-- ParseNetscapeMIMETypesEntry\n"));
NS_ASSERTION(!aEntry.IsEmpty(), "Empty Netscape MIME types entry being parsed.");
nsAString::const_iterator start_iter, end_iter, match_start, match_end;
@ -759,7 +907,7 @@ ParseNetscapeMIMETypesEntry(const nsAString& aEntry,
*
* major/minor ext1 ext2 ext3
*/
nsresult
static nsresult
ParseNormalMIMETypesEntry(const nsAString& aEntry,
nsAString::const_iterator& aMajorTypeStart,
nsAString::const_iterator& aMajorTypeEnd,
@ -768,7 +916,7 @@ ParseNormalMIMETypesEntry(const nsAString& aEntry,
nsAString& aExtensions,
nsAString::const_iterator& aDescriptionStart,
nsAString::const_iterator& aDescriptionEnd) {
LOG(("-- ParseNormalMIMETypesEntry\n"));
NS_ASSERTION(!aEntry.IsEmpty(), "Empty Normal MIME types entry being parsed.");
nsAString::const_iterator start_iter, end_iter, iter;
@ -796,12 +944,13 @@ ParseNormalMIMETypesEntry(const nsAString& aEntry,
iter = start_iter;
// get the major type
while (iter != end_iter && *iter != '/') {
++iter;
}
if (iter == end_iter) {
if (! FindCharInReadable('/', iter, end_iter))
return NS_ERROR_FAILURE;
}
nsAString::const_iterator equals_sign_iter(start_iter);
if (FindCharInReadable('=', equals_sign_iter, iter))
return NS_ERROR_FAILURE; // see bug 136670
aMajorTypeStart = start_iter;
aMajorTypeEnd = iter;
@ -830,28 +979,44 @@ ParseNormalMIMETypesEntry(const nsAString& aEntry,
}
aExtensions.Append(Substring(start_iter, iter));
if (iter != end_iter) { // not the last extension
aExtensions.Append(NS_LITERAL_STRING(","));
aExtensions.Append(PRUnichar(','));
}
}
return NS_OK;
}
nsresult
static nsresult
LookUpHandlerAndDescription(const nsAString& aMajorType,
const nsAString& aMinorType,
nsHashtable& aTypeOptions,
nsAString& aHandler,
nsAString& aDescription,
nsAString& aMozillaFlags) {
LOG(("-- LookUpHandlerAndDescription for type '%s/%s'\n",
NS_LossyConvertUCS2toASCII(aMajorType).get(),
NS_LossyConvertUCS2toASCII(aMinorType).get()));
nsresult rv = NS_OK;
nsXPIDLString mailcapFileName;
nsCOMPtr<nsIPref> thePrefsService(do_GetService(NS_PREF_CONTRACTID));
if (thePrefsService) {
rv =
thePrefsService->CopyUnicharPref("helpers.private_mailcap_file",
getter_Copies(mailcapFileName));
rv = GetFileLocation("helpers.private_mailcap_file",
"PERSONAL_MAILCAP",
getter_Copies(mailcapFileName));
if (NS_SUCCEEDED(rv) && !mailcapFileName.IsEmpty()) {
rv = GetHandlerAndDescriptionFromMailcapFile(mailcapFileName,
aMajorType,
aMinorType,
aTypeOptions,
aHandler,
aDescription,
aMozillaFlags);
} else {
rv = NS_ERROR_NOT_AVAILABLE;
}
if (NS_FAILED(rv) || aHandler.IsEmpty()) {
rv = GetFileLocation("helpers.global_mailcap_file",
"MAILCAP",
getter_Copies(mailcapFileName));
if (NS_SUCCEEDED(rv) && !mailcapFileName.IsEmpty()) {
rv = GetHandlerAndDescriptionFromMailcapFile(mailcapFileName,
aMajorType,
@ -860,28 +1025,14 @@ LookUpHandlerAndDescription(const nsAString& aMajorType,
aHandler,
aDescription,
aMozillaFlags);
} else {
rv = NS_ERROR_NOT_AVAILABLE;
}
if (aHandler.IsEmpty()) {
rv =
thePrefsService->CopyUnicharPref("helpers.global_mailcap_file",
getter_Copies(mailcapFileName));
if (NS_SUCCEEDED(rv) && !mailcapFileName.IsEmpty()) {
rv = GetHandlerAndDescriptionFromMailcapFile(mailcapFileName,
aMajorType,
aMinorType,
aTypeOptions,
aHandler,
aDescription,
aMozillaFlags);
}
}
} else {
rv = NS_ERROR_NOT_AVAILABLE;
}
return rv;
}
nsresult
static nsresult
GetHandlerAndDescriptionFromMailcapFile(const nsAString& aFilename,
const nsAString& aMajorType,
const nsAString& aMinorType,
@ -890,6 +1041,13 @@ GetHandlerAndDescriptionFromMailcapFile(const nsAString& aFilename,
nsAString& aDescription,
nsAString& aMozillaFlags) {
LOG(("-- GetHandlerAndDescriptionFromMailcapFile\n"));
LOG(("Getting handler and description from mailcap file '%s'\n",
NS_LossyConvertUCS2toASCII(aFilename).get()));
LOG(("Using type '%s/%s'\n",
NS_LossyConvertUCS2toASCII(aMajorType).get(),
NS_LossyConvertUCS2toASCII(aMinorType).get()));
nsresult rv = NS_OK;
PRBool more = PR_FALSE;
@ -909,13 +1067,8 @@ GetHandlerAndDescriptionFromMailcapFile(const nsAString& aFilename,
nsCOMPtr<nsILineInputStream> mailcap (do_QueryInterface(mailcapFile, &rv));
#ifdef DEBUG_bzbarsky
if (NS_FAILED(rv)) {
fprintf(stderr, "Interface trouble!\n\n");
}
#endif // DEBUG_bzbarsky
if (NS_FAILED(rv)) {
LOG(("Interface trouble in stream land!"));
return rv;
}
@ -936,6 +1089,9 @@ GetHandlerAndDescriptionFromMailcapFile(const nsAString& aFilename,
entry.Truncate(entry.Length()-1);
entry.Append(PRUnichar(' ')); // in case there is no trailing whitespace on this line
} else { // we have a full entry in entry. Check it for the type
LOG(("Current entry: '%s'\n",
NS_LossyConvertUCS2toASCII(entry).get()));
nsAString::const_iterator semicolon_iter,
start_iter, end_iter,
majorTypeStart, majorTypeEnd,
@ -951,11 +1107,9 @@ GetHandlerAndDescriptionFromMailcapFile(const nsAString& aFilename,
Substring(majorTypeStart,
majorTypeEnd).Equals(aMajorType,
nsCaseInsensitiveStringComparator()) &&
(Substring(minorTypeStart,
minorTypeEnd).Equals(NS_LITERAL_STRING("*")) ||
Substring(minorTypeStart,
minorTypeEnd).Equals(aMinorType,
nsCaseInsensitiveStringComparator()))) { // we have a match
Substring(minorTypeStart,
minorTypeEnd).Equals(aMinorType,
nsCaseInsensitiveStringComparator())) { // we have a match
PRBool match = PR_TRUE;
++semicolon_iter; // point at the first char past the semicolon
start_iter = semicolon_iter; // handler string starts here
@ -965,9 +1119,9 @@ GetHandlerAndDescriptionFromMailcapFile(const nsAString& aFilename,
++start_iter;
}
#ifdef DEBUG_bzbarsky
fprintf(stderr, "The real handler is: %s\n", NS_ConvertUCS2toUTF8(Substring(start_iter, semicolon_iter)).get());
#endif // DEBUG_bzbarsky
LOG(("The real handler is: '%s'\n",
NS_LossyConvertUCS2toASCII(Substring(start_iter,
semicolon_iter)).get()));
// XXX ugly hack. Just grab the executable name
nsAString::const_iterator end_handler_iter = semicolon_iter;
@ -1031,9 +1185,8 @@ GetHandlerAndDescriptionFromMailcapFile(const nsAString& aFilename,
aMinorType,
aTypeOptions,
testCommand);
#ifdef DEBUG_bzbarsky
fprintf(stderr, "Running Test: %s\n", testCommand.get());
#endif
LOG(("Running Test: %s\n", testCommand.get()));
// XXX this should not use system(), since that can block the UI thread!
if (NS_SUCCEEDED(rv) && system(testCommand.get()) != 0) {
match = PR_FALSE;
}
@ -1060,6 +1213,7 @@ GetHandlerAndDescriptionFromMailcapFile(const nsAString& aFilename,
}
}
if (!more) {
rv = NS_ERROR_NOT_AVAILABLE;
break;
}
rv = mailcap->ReadLine(buffer, &more);
@ -1070,6 +1224,8 @@ GetHandlerAndDescriptionFromMailcapFile(const nsAString& aFilename,
NS_IMETHODIMP nsOSHelperAppService::ExternalProtocolHandlerExists(const char * aProtocolScheme, PRBool * aHandlerExists)
{
LOG(("-- nsOSHelperAppService::ExternalProtocolHandlerExists for '%s'\n",
aProtocolScheme));
// look up the protocol scheme in the windows registry....if we find a match then we have a handler for it...
*aHandlerExists = PR_FALSE;
return NS_OK;
@ -1077,11 +1233,14 @@ NS_IMETHODIMP nsOSHelperAppService::ExternalProtocolHandlerExists(const char * a
NS_IMETHODIMP nsOSHelperAppService::LoadUrl(nsIURI * aURL)
{
LOG(("-- nsOSHelperAppService::LoadUrl\n"));
return NS_ERROR_NOT_IMPLEMENTED;
}
nsresult nsOSHelperAppService::GetFileTokenForPath(const PRUnichar * platformAppPath, nsIFile ** aFile)
{
LOG(("-- nsOSHelperAppService::GetFileTokenForPath: '%s'\n",
NS_LossyConvertUCS2toASCII(platformAppPath).get()));
if (! *platformAppPath) { // empty filename--return error
NS_WARNING("Empty filename passed in.");
return NS_ERROR_INVALID_ARG;
@ -1150,9 +1309,7 @@ NS_IMETHODIMP nsOSHelperAppService::GetFromExtension(const char *aFileExt,
nsresult rv = nsExternalHelperAppService::GetFromExtension(aFileExt, _retval);
if (NS_SUCCEEDED(rv) && *_retval) return NS_OK; // okay we got an entry so we are done.
#ifdef DEBUG_bzbarsky
fprintf(stderr, "Here we do an extension lookup for %s\n\n", aFileExt);
#endif // DEBUG_bzbarsky
LOG(("Here we do an extension lookup for '%s'\n", aFileExt));
nsAutoString mimeType, majorType, minorType,
mime_types_description, mailcap_description,
@ -1162,10 +1319,14 @@ NS_IMETHODIMP nsOSHelperAppService::GetFromExtension(const char *aFileExt,
majorType,
minorType,
mime_types_description);
if (NS_FAILED(rv))
return rv;
LOG(("Type/Description results: majorType='%s', minorType='%s', description='%s'\n",
NS_LossyConvertUCS2toASCII(majorType).get(),
NS_LossyConvertUCS2toASCII(minorType).get(),
NS_LossyConvertUCS2toASCII(mime_types_description).get()));
if (majorType.IsEmpty() && minorType.IsEmpty()) {
// we didn't get a type mapping, so we can't do anything useful
return NS_ERROR_FAILURE;
@ -1179,9 +1340,28 @@ NS_IMETHODIMP nsOSHelperAppService::GetFromExtension(const char *aFileExt,
mimeInfo->SetMIMEType(NS_ConvertUCS2toUTF8(mimeType).get());
mimeInfo->AppendExtension(aFileExt);
nsHashtable typeOptions; // empty hash table
/*
The mailcap lookup is two-pass to handle the case of mailcap files
that have something like:
text/*; emacs %s
text/rtf; soffice %s
in that order. We want to pick up "soffice" for text/rtf in such cases
*/
rv = LookUpHandlerAndDescription(majorType, minorType, typeOptions,
handler, mailcap_description,
mozillaFlags);
if (NS_FAILED(rv)) {
// maybe we have an entry for "majorType/*"?
rv = LookUpHandlerAndDescription(majorType, NS_LITERAL_STRING("*"),
typeOptions, handler, mailcap_description,
mozillaFlags);
}
LOG(("Handler/Description results: handler='%s', description='%s', mozillaFlags='%s'\n",
NS_LossyConvertUCS2toASCII(handler).get(),
NS_LossyConvertUCS2toASCII(mailcap_description).get(),
NS_LossyConvertUCS2toASCII(mozillaFlags).get()));
mailcap_description.Trim(" \t\"");
mozillaFlags.Trim(" \t");
if (!mime_types_description.IsEmpty()) {
@ -1196,9 +1376,6 @@ NS_IMETHODIMP nsOSHelperAppService::GetFromExtension(const char *aFileExt,
if (NS_SUCCEEDED(rv)) {
mimeInfo->SetPreferredApplicationHandler(handlerFile);
mimeInfo->SetPreferredAction(nsIMIMEInfo::useHelperApp);
#ifdef DEBUG_bzbarsky
fprintf(stderr, "Here we want to set handler to: %s\n", NS_ConvertUCS2toUTF8(handler).get());
#endif // DEBUG_bzbarsky
mimeInfo->SetApplicationDescription(handler.get());
}
} else {
@ -1223,9 +1400,7 @@ NS_IMETHODIMP nsOSHelperAppService::GetFromMIMEType(const char *aMIMEType,
// first, see if the base class already has an entry....
nsresult rv = nsExternalHelperAppService::GetFromMIMEType(aMIMEType, _retval);
if (NS_SUCCEEDED(rv) && *_retval) return NS_OK; // okay we got an entry so we are done.
#ifdef DEBUG_bzbarsky
fprintf(stderr, "Here we do a mimetype lookup for %s\n\n\n", aMIMEType);
#endif // DEBUG_bzbarsky
LOG(("Here we do a mimetype lookup for '%s'\n", aMIMEType));
nsAutoString extensions,
mime_types_description, mailcap_description,
handler, mozillaFlags;
@ -1252,12 +1427,34 @@ NS_IMETHODIMP nsOSHelperAppService::GetFromMIMEType(const char *aMIMEType,
nsDependentSubstring majorType(majorTypeStart, majorTypeEnd);
nsDependentSubstring minorType(minorTypeStart, minorTypeEnd);
LookUpHandlerAndDescription(majorType,
minorType,
typeOptions,
handler,
mailcap_description,
mozillaFlags);
/*
The mailcap lookup is two-pass to handle the case of mailcap files
that have something like:
text/*; emacs %s
text/rtf; soffice %s
in that order. We want to pick up "soffice" for text/rtf in such cases
*/
rv = LookUpHandlerAndDescription(majorType,
minorType,
typeOptions,
handler,
mailcap_description,
mozillaFlags);
if (NS_FAILED(rv)) {
// maybe we have an entry for "majorType/*"?
rv = LookUpHandlerAndDescription(majorType,
NS_LITERAL_STRING("*"),
typeOptions,
handler,
mailcap_description,
mozillaFlags);
}
LOG(("Handler/Description results: handler='%s', description='%s', mozillaFlags='%s'\n",
NS_LossyConvertUCS2toASCII(handler).get(),
NS_LossyConvertUCS2toASCII(mailcap_description).get(),
NS_LossyConvertUCS2toASCII(mozillaFlags).get()));
if (handler.IsEmpty()) {
// we have no useful info....
@ -1291,10 +1488,6 @@ NS_IMETHODIMP nsOSHelperAppService::GetFromMIMEType(const char *aMIMEType,
if (NS_SUCCEEDED(rv)) {
mimeInfo->SetPreferredApplicationHandler(handlerFile);
mimeInfo->SetPreferredAction(nsIMIMEInfo::useHelperApp);
// FIXME set the handler
#ifdef DEBUG_bzbarsky
fprintf(stderr, "Here we want to set handler to: %s\n", NS_ConvertUCS2toUTF8(handler).get());
#endif // DEBUG_bzbarsky
mimeInfo->SetApplicationDescription(handler.get());
} else {
mimeInfo->SetPreferredAction(nsIMIMEInfo::saveToDisk);