From 1c61776f7af1296fc35e358c379c70274cc5d494 Mon Sep 17 00:00:00 2001 From: "bzbarsky%mit.edu" Date: Wed, 16 Jan 2002 04:28:06 +0000 Subject: [PATCH] Bug 90633. Use the URL extension for the temp file we pass to helpers if it matches the extension list in the mime info. r=law, sr=mscott. --- .../ui/composer/content/ComposerCommands.js | 2 +- .../src/nsWebBrowserPersist.cpp | 2 +- mailnews/mime/src/mimedrft.cpp | 2 +- mailnews/mime/src/mimemoz2.cpp | 2 +- .../decoders/icon/win/nsIconChannel.cpp | 2 +- netwerk/mime/public/nsIMIMEInfo.idl | 14 +++++----- netwerk/mime/src/nsMIMEInfoImpl.cpp | 28 +++++++++++++++++-- .../exthandler/nsExternalHelperAppService.cpp | 20 +++++++++++-- uriloader/exthandler/nsMIMEInfoImpl.cpp | 28 +++++++++++++++++-- .../exthandler/os2/nsOSHelperAppService.cpp | 2 +- .../content/pref-applications-new.js | 2 +- 11 files changed, 84 insertions(+), 20 deletions(-) diff --git a/editor/ui/composer/content/ComposerCommands.js b/editor/ui/composer/content/ComposerCommands.js index 6935efd034e..dc43e12b68a 100644 --- a/editor/ui/composer/content/ComposerCommands.js +++ b/editor/ui/composer/content/ComposerCommands.js @@ -359,7 +359,7 @@ function GetExtensionBasedOnMimeType(aMIMEType) var mimeInfo = mimeService.GetFromMIMEType(aMIMEType); if (!mimeInfo) return ""; - var fileExtension = mimeInfo.FirstExtension(); + var fileExtension = mimeInfo.primaryExtension; // the MIME service likes to give back ".htm" for text/html files, // so do a special-case fix here. diff --git a/embedding/components/webbrowserpersist/src/nsWebBrowserPersist.cpp b/embedding/components/webbrowserpersist/src/nsWebBrowserPersist.cpp index 2833467ec26..c89cce021d7 100644 --- a/embedding/components/webbrowserpersist/src/nsWebBrowserPersist.cpp +++ b/embedding/components/webbrowserpersist/src/nsWebBrowserPersist.cpp @@ -1002,7 +1002,7 @@ nsWebBrowserPersist::CalculateAndAppendFileExt(nsIURI *aURI, nsIChannel *aChanne // Append the mime file extension nsXPIDLCString fileExt; if (!hasExtension && - NS_SUCCEEDED(mimeInfo->FirstExtension(getter_Copies(fileExt)))) + NS_SUCCEEDED(mimeInfo->GetPrimaryExtension(getter_Copies(fileExt)))) { newFileName.Append("."); newFileName.Append(fileExt.get()); diff --git a/mailnews/mime/src/mimedrft.cpp b/mailnews/mime/src/mimedrft.cpp index 9d106bae12d..9d54579f876 100644 --- a/mailnews/mime/src/mimedrft.cpp +++ b/mailnews/mime/src/mimedrft.cpp @@ -1849,7 +1849,7 @@ mime_decompose_file_init_fn ( void *stream_closure, MimeHeaders *headers ) { nsXPIDLCString fileExtension; - if ( (NS_SUCCEEDED(mimeInfo->FirstExtension(getter_Copies(fileExtension)))) && fileExtension) + if ( (NS_SUCCEEDED(mimeInfo->GetPrimaryExtension(getter_Copies(fileExtension)))) && fileExtension) { newAttachName.Append("."); newAttachName.Append(fileExtension); diff --git a/mailnews/mime/src/mimemoz2.cpp b/mailnews/mime/src/mimemoz2.cpp index 10a81d201ec..2f5c4010f77 100644 --- a/mailnews/mime/src/mimemoz2.cpp +++ b/mailnews/mime/src/mimemoz2.cpp @@ -273,7 +273,7 @@ ValidateRealName(nsMsgAttachmentData *aAttach, MimeHeaders *aHdrs) { char *aFileExtension = nsnull; - if ( (NS_SUCCEEDED(mimeInfo->FirstExtension(&aFileExtension))) && aFileExtension) + if ( (NS_SUCCEEDED(mimeInfo->GetPrimaryExtension(&aFileExtension))) && aFileExtension) { newAttachName.Append(NS_LITERAL_STRING(".")); newAttachName.AppendWithConversion(aFileExtension); diff --git a/modules/libpr0n/decoders/icon/win/nsIconChannel.cpp b/modules/libpr0n/decoders/icon/win/nsIconChannel.cpp index ed6940daa14..2e4bddc0e32 100644 --- a/modules/libpr0n/decoders/icon/win/nsIconChannel.cpp +++ b/modules/libpr0n/decoders/icon/win/nsIconChannel.cpp @@ -334,7 +334,7 @@ NS_IMETHODIMP nsIconChannel::AsyncOpen(nsIStreamListener *aListener, nsISupports if (mimeObject) { nsXPIDLCString fileExt; - mimeObject->FirstExtension(getter_Copies(fileExt)); + mimeObject->GetPrimaryExtension(getter_Copies(fileExt)); // we need to insert a '.' b4 the extension... nsCAutoString formattedFileExt; formattedFileExt = "."; diff --git a/netwerk/mime/public/nsIMIMEInfo.idl b/netwerk/mime/public/nsIMIMEInfo.idl index 2136540fa47..1a7746974a8 100644 --- a/netwerk/mime/public/nsIMIMEInfo.idl +++ b/netwerk/mime/public/nsIMIMEInfo.idl @@ -72,13 +72,6 @@ interface nsIMIMEInfo : nsISupports { boolean ExtensionExists(in string aExtension); - /* Returns the first extension association in - * the internal set of extensions. - * - * @return The first extension. - */ - string FirstExtension(); - /* * Append a given extension to the set of extensions */ @@ -91,6 +84,13 @@ interface nsIMIMEInfo : nsISupports { nsIMIMEInfo clone(); + /* Returns the first extension association in + * the internal set of extensions. + * + * @return The first extension. + */ + attribute string primaryExtension; + /* The MIME type of this MIMEInfo. * * @return String representing the MIME type. diff --git a/netwerk/mime/src/nsMIMEInfoImpl.cpp b/netwerk/mime/src/nsMIMEInfoImpl.cpp index 988e1275159..a113890d2fa 100644 --- a/netwerk/mime/src/nsMIMEInfoImpl.cpp +++ b/netwerk/mime/src/nsMIMEInfoImpl.cpp @@ -98,9 +98,10 @@ nsMIMEInfoImpl::ExtensionExists(const char *aExtension, PRBool *_retval) { if (!aExtension) return NS_ERROR_NULL_POINTER; + nsDependentCString extension(aExtension); for (PRUint8 i=0; i < extCount; i++) { nsCString* ext = (nsCString*)mExtensions.CStringAt(i); - if (ext->Equals(aExtension)) { + if (ext->Equals(extension, nsCaseInsensitiveCStringComparator())) { found = PR_TRUE; break; } @@ -111,7 +112,7 @@ nsMIMEInfoImpl::ExtensionExists(const char *aExtension, PRBool *_retval) { } NS_IMETHODIMP -nsMIMEInfoImpl::FirstExtension(char **_retval) { +nsMIMEInfoImpl::GetPrimaryExtension(char **_retval) { PRUint32 extCount = mExtensions.Count(); if (extCount < 1) return NS_ERROR_NOT_INITIALIZED; @@ -120,6 +121,29 @@ nsMIMEInfoImpl::FirstExtension(char **_retval) { return NS_OK; } +NS_IMETHODIMP +nsMIMEInfoImpl::SetPrimaryExtension(const char *aExtension) { + NS_ASSERTION(aExtension, "no extension"); + PRUint32 extCount = mExtensions.Count(); + nsCString extension(aExtension); + PRUint8 i; + PRBool found = PR_FALSE; + for (i=0; i < extCount; i++) { + nsCString* ext = (nsCString*)mExtensions.CStringAt(i); + if (ext->Equals(extension, nsCaseInsensitiveCStringComparator())) { + found = PR_TRUE; + break; + } + } + if (found) { + mExtensions.RemoveCStringAt(i); + } + + mExtensions.InsertCStringAt(extension, 0); + + return NS_OK; +} + NS_IMETHODIMP nsMIMEInfoImpl::AppendExtension(const char *aExtension) { mExtensions.AppendCString( nsCAutoString(aExtension) ); diff --git a/uriloader/exthandler/nsExternalHelperAppService.cpp b/uriloader/exthandler/nsExternalHelperAppService.cpp index 8fafbfe7681..8452c4e06fc 100644 --- a/uriloader/exthandler/nsExternalHelperAppService.cpp +++ b/uriloader/exthandler/nsExternalHelperAppService.cpp @@ -275,9 +275,25 @@ NS_IMETHODIMP nsExternalHelperAppService::DoContent(const char *aMimeContentType *aStreamListener = nsnull; if (mimeInfo) { - // ensure that the file extension field is always filled in - mimeInfo->FirstExtension(getter_Copies(fileExtension)); + // The primary extension for the mime info may be different from + // the URL extension. If the URL extension matches the mime info, + // set it as the primary extension. In either case, fileExtension + // should be the primary extension once we are doen. + if (fileExtension.IsEmpty()) { + nsCOMPtr url = do_QueryInterface(aURI); + if (url) { + url->GetFileExtension(getter_Copies(fileExtension)); + } + } + PRBool matches = PR_FALSE; + mimeInfo->ExtensionExists(fileExtension.get(), &matches); + if (matches) { + mimeInfo->SetPrimaryExtension(fileExtension.get()); + } else { + mimeInfo->GetPrimaryExtension(getter_Copies(fileExtension)); + } + // this code is incomplete and just here to get things started.. nsExternalAppHandler * handler = CreateNewExternalHandler(mimeInfo, fileExtension, aWindowContext); handler->QueryInterface(NS_GET_IID(nsIStreamListener), (void **) aStreamListener); diff --git a/uriloader/exthandler/nsMIMEInfoImpl.cpp b/uriloader/exthandler/nsMIMEInfoImpl.cpp index 988e1275159..a113890d2fa 100644 --- a/uriloader/exthandler/nsMIMEInfoImpl.cpp +++ b/uriloader/exthandler/nsMIMEInfoImpl.cpp @@ -98,9 +98,10 @@ nsMIMEInfoImpl::ExtensionExists(const char *aExtension, PRBool *_retval) { if (!aExtension) return NS_ERROR_NULL_POINTER; + nsDependentCString extension(aExtension); for (PRUint8 i=0; i < extCount; i++) { nsCString* ext = (nsCString*)mExtensions.CStringAt(i); - if (ext->Equals(aExtension)) { + if (ext->Equals(extension, nsCaseInsensitiveCStringComparator())) { found = PR_TRUE; break; } @@ -111,7 +112,7 @@ nsMIMEInfoImpl::ExtensionExists(const char *aExtension, PRBool *_retval) { } NS_IMETHODIMP -nsMIMEInfoImpl::FirstExtension(char **_retval) { +nsMIMEInfoImpl::GetPrimaryExtension(char **_retval) { PRUint32 extCount = mExtensions.Count(); if (extCount < 1) return NS_ERROR_NOT_INITIALIZED; @@ -120,6 +121,29 @@ nsMIMEInfoImpl::FirstExtension(char **_retval) { return NS_OK; } +NS_IMETHODIMP +nsMIMEInfoImpl::SetPrimaryExtension(const char *aExtension) { + NS_ASSERTION(aExtension, "no extension"); + PRUint32 extCount = mExtensions.Count(); + nsCString extension(aExtension); + PRUint8 i; + PRBool found = PR_FALSE; + for (i=0; i < extCount; i++) { + nsCString* ext = (nsCString*)mExtensions.CStringAt(i); + if (ext->Equals(extension, nsCaseInsensitiveCStringComparator())) { + found = PR_TRUE; + break; + } + } + if (found) { + mExtensions.RemoveCStringAt(i); + } + + mExtensions.InsertCStringAt(extension, 0); + + return NS_OK; +} + NS_IMETHODIMP nsMIMEInfoImpl::AppendExtension(const char *aExtension) { mExtensions.AppendCString( nsCAutoString(aExtension) ); diff --git a/uriloader/exthandler/os2/nsOSHelperAppService.cpp b/uriloader/exthandler/os2/nsOSHelperAppService.cpp index 434ff127c0e..cfc7ae3ed7c 100644 --- a/uriloader/exthandler/os2/nsOSHelperAppService.cpp +++ b/uriloader/exthandler/os2/nsOSHelperAppService.cpp @@ -52,7 +52,7 @@ nsresult nsOSHelperAppService::FindOSMimeInfoForType(const char * aMimeContentTy if (mimeInfo) { nsXPIDLCString mimefileExt; - mimeInfo->FirstExtension(getter_Copies(mimefileExt)); + mimeInfo->GetPrimaryExtension(getter_Copies(mimefileExt)); fileExtension = "."; fileExtension.Append(mimefileExt); } diff --git a/xpfe/components/prefwindow/resources/content/pref-applications-new.js b/xpfe/components/prefwindow/resources/content/pref-applications-new.js index e9e16b94265..047d1e1f469 100644 --- a/xpfe/components/prefwindow/resources/content/pref-applications-new.js +++ b/xpfe/components/prefwindow/resources/content/pref-applications-new.js @@ -43,7 +43,7 @@ function Startup() // Fill the fields we can from this. gDescriptionField.value = info.Description; - gExtensionField.value = info.FirstExtension(); + gExtensionField.value = info.primaryExtension; gMIMEField.value = info.MIMEType; var app = info.preferredApplicationHandler; if ( app ) {