mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-29 07:42:04 +00:00
Bug 80787 support non-ascii extensions in mime service, mime info, and
exthandler r=bzbarsky sr=darin
This commit is contained in:
parent
ef29a93fb9
commit
8e63886ee0
@ -67,7 +67,7 @@ interface nsIMIMEInfo : nsISupports {
|
||||
/**
|
||||
* Set File Extensions. Input is a comma delimited list of extensions.
|
||||
*/
|
||||
void setFileExtensions(in ACString aExtensions);
|
||||
void setFileExtensions(in AUTF8String aExtensions);
|
||||
|
||||
/**
|
||||
* Returns whether or not the given extension is
|
||||
@ -75,12 +75,12 @@ interface nsIMIMEInfo : nsISupports {
|
||||
*
|
||||
* @return TRUE if the association exists.
|
||||
*/
|
||||
boolean extensionExists(in ACString aExtension);
|
||||
boolean extensionExists(in AUTF8String aExtension);
|
||||
|
||||
/**
|
||||
* Append a given extension to the set of extensions
|
||||
*/
|
||||
void appendExtension(in ACString aExtension);
|
||||
void appendExtension(in AUTF8String aExtension);
|
||||
|
||||
/**
|
||||
* Returns the first extension association in
|
||||
@ -88,7 +88,7 @@ interface nsIMIMEInfo : nsISupports {
|
||||
*
|
||||
* @return The first extension.
|
||||
*/
|
||||
attribute ACString primaryExtension;
|
||||
attribute AUTF8String primaryExtension;
|
||||
|
||||
/**
|
||||
* The MIME type of this MIMEInfo.
|
||||
|
@ -70,7 +70,7 @@ interface nsIMIMEService : nsISupports {
|
||||
* can be an empty string. At least one of aMIMEType and aFileExt
|
||||
* must be nonempty.
|
||||
*/
|
||||
nsIMIMEInfo getFromTypeAndExtension(in ACString aMIMEType, in ACString aFileExt);
|
||||
nsIMIMEInfo getFromTypeAndExtension(in ACString aMIMEType, in AUTF8String aFileExt);
|
||||
|
||||
/**
|
||||
* Retrieves a ACString representation of the MIME type
|
||||
@ -79,7 +79,7 @@ interface nsIMIMEService : nsISupports {
|
||||
* @param A file extension (excluding the dot ('.')).
|
||||
* @return The MIME type, if any.
|
||||
*/
|
||||
ACString getTypeFromExtension(in ACString aFileExt);
|
||||
ACString getTypeFromExtension(in AUTF8String aFileExt);
|
||||
|
||||
/**
|
||||
* Retrieves a ACString representation of the MIME type
|
||||
@ -102,5 +102,5 @@ interface nsIMIMEService : nsISupports {
|
||||
* @param aMIMEType The Type to get information on. Must not be empty.
|
||||
* @param aFileExt File Extension. Can be empty.
|
||||
*/
|
||||
ACString getPrimaryExtension(in ACString aMIMEType, in ACString aFileExt);
|
||||
AUTF8String getPrimaryExtension(in ACString aMIMEType, in AUTF8String aFileExt);
|
||||
};
|
||||
|
@ -49,7 +49,6 @@
|
||||
#include "nsAppDirectoryServiceDefs.h"
|
||||
#include "nsICategoryManager.h"
|
||||
#include "nsXPIDLString.h"
|
||||
#include "nsReadableUtils.h"
|
||||
#include "nsUnicharUtils.h"
|
||||
#include "nsIStringEnumerator.h"
|
||||
#include "nsMemory.h"
|
||||
@ -73,8 +72,8 @@
|
||||
#include "nsHelperAppRDF.h"
|
||||
#include "nsIMIMEInfo.h"
|
||||
#include "nsDirectoryServiceDefs.h"
|
||||
#include "nsIRefreshURI.h"
|
||||
#include "nsIDocumentLoader.h"
|
||||
#include "nsIRefreshURI.h" // XXX needed to redirect according to Refresh: URI
|
||||
#include "nsIDocumentLoader.h" // XXX needed to get orig. channel and assoc. refresh uri
|
||||
#include "nsIHelperAppLauncherDialog.h"
|
||||
#include "nsNetUtil.h"
|
||||
#include "nsIIOService.h"
|
||||
@ -85,7 +84,6 @@
|
||||
#include "nsIHttpChannel.h"
|
||||
#include "nsIEncodedChannel.h"
|
||||
#include "nsIMultiPartChannel.h"
|
||||
#include "nsIAtom.h"
|
||||
#include "nsIObserverService.h" // so we can be a profile change observer
|
||||
|
||||
#if defined(XP_MAC) || defined (XP_MACOSX)
|
||||
@ -94,22 +92,21 @@
|
||||
#include "nsIAppleFileDecoder.h"
|
||||
#endif // defined(XP_MAC) || defined (XP_MACOSX)
|
||||
|
||||
#include "nsIPluginHost.h"
|
||||
#include "nsIPluginHost.h" // XXX needed for ext->type mapping (bug 233289)
|
||||
#include "nsEscape.h"
|
||||
|
||||
#include "nsIStringBundle.h"
|
||||
#include "nsIStringBundle.h" // XXX needed to localize error msgs
|
||||
#include "nsIPromptService.h"
|
||||
#include "nsIDOMWindow.h"
|
||||
#include "nsIDOMWindow.h" // XXX needed to get parent for error dlg
|
||||
|
||||
#include "nsITextToSubURI.h"
|
||||
#include "nsITextToSubURI.h" // to unescape the filename
|
||||
#include "nsIMIMEHeaderParam.h"
|
||||
|
||||
#include "nsIPrefService.h"
|
||||
|
||||
#include "nsIGlobalHistory.h"
|
||||
#include "nsIGlobalHistory.h" // to mark downloads as visited
|
||||
|
||||
#include "nsCRT.h"
|
||||
#include "plstr.h"
|
||||
|
||||
#ifdef PR_LOGGING
|
||||
PRLogModuleInfo* nsExternalHelperAppService::mLog = nsnull;
|
||||
@ -135,6 +132,47 @@ static nsExternalHelperAppService* sSrv;
|
||||
|
||||
// Helper functions for Content-Disposition headers
|
||||
|
||||
/**
|
||||
* Given a URI fragment, unescape it
|
||||
* @param aFragment The string to unescape
|
||||
* @param aURI The URI from which this fragment is taken. Only its character set
|
||||
* will be used.
|
||||
* @param aResult [out] Unescaped string.
|
||||
*/
|
||||
static nsresult UnescapeFragment(const nsACString& aFragment, nsIURI* aURI,
|
||||
nsAString& aResult)
|
||||
{
|
||||
// First, we need a charset
|
||||
nsCAutoString originCharset;
|
||||
nsresult rv = aURI->GetOriginCharset(originCharset);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
// Now, we need the unescaper
|
||||
nsCOMPtr<nsITextToSubURI> textToSubURI = do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
return textToSubURI->UnEscapeURIForUI(originCharset, aFragment, aResult);
|
||||
}
|
||||
|
||||
/**
|
||||
* UTF-8 version of UnescapeFragment.
|
||||
* @param aFragment The string to unescape
|
||||
* @param aURI The URI from which this fragment is taken. Only its character set
|
||||
* will be used.
|
||||
* @param aResult [out] Unescaped string, UTF-8 encoded.
|
||||
* @note It is safe to pass the same string for aFragment and aResult.
|
||||
* @note When this function fails, aResult will not be modified.
|
||||
*/
|
||||
static nsresult UnescapeFragment(const nsACString& aFragment, nsIURI* aURI,
|
||||
nsACString& aResult)
|
||||
{
|
||||
nsAutoString result;
|
||||
nsresult rv = UnescapeFragment(aFragment, aURI, result);
|
||||
if (NS_SUCCEEDED(rv))
|
||||
CopyUTF16toUTF8(result, aResult);
|
||||
return rv;
|
||||
}
|
||||
|
||||
/** Gets the content-disposition header from a channel, using nsIHttpChannel
|
||||
* or nsIMultipartChannel if available
|
||||
* @param aChannel The channel to extract the disposition header from
|
||||
@ -206,7 +244,7 @@ static void GetFilenameFromDisposition(nsAString& aFilename,
|
||||
* WARNING - this filename may contain characters which the OS does not
|
||||
* allow as part of filenames!
|
||||
* @param aExtension [out] Reference to the string where the extension should
|
||||
* be stored. Empty if it could not be retrieved.
|
||||
* be stored. Empty if it could not be retrieved. Stored in UTF-8.
|
||||
* @param aAllowURLExtension (optional) Get the extension from the URL if no
|
||||
* Content-Disposition header is present. Default is true.
|
||||
* @retval true The server sent Content-Disposition:attachment or equivalent
|
||||
@ -270,6 +308,8 @@ static PRBool GetFilenameAndExtensionFromChannel(nsIChannel* aChannel,
|
||||
{
|
||||
if (aAllowURLExtension) {
|
||||
url->GetFileExtension(aExtension);
|
||||
UnescapeFragment(aExtension, url, aExtension);
|
||||
|
||||
// Windows ignores terminating dots. So we have to as well, so
|
||||
// that our security checks do "the right thing"
|
||||
// In case the aExtension consisted only of the dot, the code below will
|
||||
@ -279,19 +319,11 @@ static PRBool GetFilenameAndExtensionFromChannel(nsIChannel* aChannel,
|
||||
|
||||
// try to extract the file name from the url and use that as a first pass as the
|
||||
// leaf name of our temp file...
|
||||
nsCAutoString leafName; // may be shortened by NS_UnescapeURL
|
||||
nsCAutoString leafName;
|
||||
url->GetFileName(leafName);
|
||||
if (!leafName.IsEmpty())
|
||||
{
|
||||
nsCOMPtr<nsITextToSubURI> textToSubURI = do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv);
|
||||
if (NS_SUCCEEDED(rv))
|
||||
{
|
||||
nsCAutoString originCharset;
|
||||
url->GetOriginCharset(originCharset);
|
||||
rv = textToSubURI->UnEscapeURIForUI(originCharset, leafName,
|
||||
aFileName);
|
||||
}
|
||||
|
||||
rv = UnescapeFragment(leafName, url, aFileName);
|
||||
if (NS_FAILED(rv))
|
||||
{
|
||||
CopyUTF8toUTF16(leafName, aFileName); // use escaped name
|
||||
@ -372,7 +404,7 @@ struct nsExtraMimeTypeEntry {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This table lists all of the 'extra" content types that we can deduce from particular
|
||||
* This table lists all of the 'extra' content types that we can deduce from particular
|
||||
* file extensions. These entries also ensure that we provide a good descriptive name
|
||||
* when we encounter files with these content types and/or extensions. These can be
|
||||
* overridden by user helper app prefs.
|
||||
@ -415,12 +447,13 @@ static nsExtraMimeTypeEntry extraMimeEntries [] =
|
||||
|
||||
/**
|
||||
* File extensions for which decoding should be disabled.
|
||||
* NOTE: These MUST be lower-case and ASCII.
|
||||
*/
|
||||
static nsDefaultMimeTypeEntry nonDecodableExtensions [] = {
|
||||
{ APPLICATION_GZIP, "gz" },
|
||||
{ APPLICATION_GZIP, "tgz" },
|
||||
{ APPLICATION_ZIP, "zip" },
|
||||
{ APPLICATION_COMPRESS, "Z" }
|
||||
{ APPLICATION_COMPRESS, "z" }
|
||||
};
|
||||
|
||||
NS_IMPL_ISUPPORTS6(
|
||||
@ -637,8 +670,8 @@ NS_IMETHODIMP nsExternalHelperAppService::ApplyDecodingForExtension(const nsACSt
|
||||
*aApplyDecoding = PR_TRUE;
|
||||
PRUint32 i;
|
||||
for(i = 0; i < NS_ARRAY_LENGTH(nonDecodableExtensions); ++i) {
|
||||
if (aExtension.Equals(nonDecodableExtensions[i].mFileExtension, nsCaseInsensitiveCStringComparator()) &&
|
||||
aEncodingType.Equals(nonDecodableExtensions[i].mMimeType, nsCaseInsensitiveCStringComparator())) {
|
||||
if (aExtension.LowerCaseEqualsASCII(nonDecodableExtensions[i].mFileExtension) &&
|
||||
aEncodingType.LowerCaseEqualsASCII(nonDecodableExtensions[i].mMimeType)) {
|
||||
*aApplyDecoding = PR_FALSE;
|
||||
break;
|
||||
}
|
||||
@ -699,7 +732,7 @@ nsresult nsExternalHelperAppService::FillTopLevelProperties(nsIRDFResource * aCo
|
||||
if (!literal) return NS_ERROR_FAILURE;
|
||||
|
||||
literal->GetValueConst(&stringValue);
|
||||
fileExtension.AssignWithConversion(stringValue);
|
||||
CopyUTF16toUTF8(stringValue, fileExtension);
|
||||
if (!fileExtension.IsEmpty())
|
||||
aMIMEInfo->AppendExtension(fileExtension);
|
||||
}
|
||||
@ -1077,7 +1110,7 @@ nsresult nsExternalHelperAppService::ExpungeTemporaryFiles()
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
// XPCOM Shutdown observer
|
||||
// XPCOM profile change observer
|
||||
NS_IMETHODIMP
|
||||
nsExternalHelperAppService::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData )
|
||||
{
|
||||
@ -1327,8 +1360,8 @@ nsresult nsExternalAppHandler::SetUpTempFile(nsIChannel * aChannel)
|
||||
#if defined(XP_MAC) || defined (XP_MACOSX)
|
||||
nsCAutoString contentType;
|
||||
mMimeInfo->GetMIMEType(contentType);
|
||||
if (contentType.EqualsIgnoreCase(APPLICATION_APPLEFILE) ||
|
||||
contentType.EqualsIgnoreCase(MULTIPART_APPLEDOUBLE))
|
||||
if (contentType.LowerCaseEqualsLiteral(APPLICATION_APPLEFILE) ||
|
||||
contentType.LowerCaseEqualsLiteral(MULTIPART_APPLEDOUBLE))
|
||||
{
|
||||
nsCOMPtr<nsIAppleFileDecoder> appleFileDecoder = do_CreateInstance(NS_IAPPLEFILEDECODER_CONTRACTID, &rv);
|
||||
if (NS_SUCCEEDED(rv) && appleFileDecoder)
|
||||
@ -1349,10 +1382,6 @@ NS_IMETHODIMP nsExternalAppHandler::OnStartRequest(nsIRequest *request, nsISuppo
|
||||
|
||||
mRequest = request;
|
||||
|
||||
// first, check to see if we've been canceled....
|
||||
if (mCanceled) // then go cancel our underlying channel too
|
||||
return request->Cancel(NS_BINDING_ABORTED);
|
||||
|
||||
nsCOMPtr<nsIChannel> aChannel = do_QueryInterface(request);
|
||||
|
||||
// Get content length and URI.
|
||||
@ -1806,7 +1835,7 @@ nsresult nsExternalAppHandler::Init(nsIMIMEInfo * aMIMEInfo,
|
||||
// make sure the extention includes the '.'
|
||||
if (!aTempFileExtension.IsEmpty() && aTempFileExtension.First() != '.')
|
||||
mTempFileExtension = PRUnichar('.');
|
||||
mTempFileExtension.AppendWithConversion(aTempFileExtension);
|
||||
AppendUTF8toUTF16(aTempFileExtension, mTempFileExtension);
|
||||
|
||||
mSuggestedFileName = aSuggestedFilename;
|
||||
|
||||
@ -2362,7 +2391,7 @@ NS_IMETHODIMP nsExternalHelperAppService::GetTypeFromExtension(const nsACString&
|
||||
// First of all, check our default entries
|
||||
for (size_t i = 0; i < NS_ARRAY_LENGTH(defaultMimeEntries); i++)
|
||||
{
|
||||
if (aFileExt.Equals(defaultMimeEntries[i].mFileExtension, nsCaseInsensitiveCStringComparator())) {
|
||||
if (aFileExt.LowerCaseEqualsASCII(defaultMimeEntries[i].mFileExtension)) {
|
||||
aContentType = defaultMimeEntries[i].mMimeType;
|
||||
return rv;
|
||||
}
|
||||
@ -2446,17 +2475,22 @@ NS_IMETHODIMP nsExternalHelperAppService::GetTypeFromURI(nsIURI *aURI, nsACStrin
|
||||
if (url) {
|
||||
nsCAutoString ext;
|
||||
rv = url->GetFileExtension(ext);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
if (ext.IsEmpty()) {
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
if (ext.IsEmpty())
|
||||
return NS_ERROR_NOT_AVAILABLE;
|
||||
}
|
||||
|
||||
UnescapeFragment(ext, url, ext);
|
||||
|
||||
return GetTypeFromExtension(ext, aContentType);
|
||||
}
|
||||
|
||||
// no url, let's give the raw spec a shot
|
||||
nsCAutoString specStr;
|
||||
rv = aURI->GetSpec(specStr);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
UnescapeFragment(specStr, aURI, specStr);
|
||||
|
||||
// find the file extension (if any)
|
||||
PRInt32 extLoc = specStr.RFindChar('.');
|
||||
@ -2480,25 +2514,24 @@ NS_IMETHODIMP nsExternalHelperAppService::GetTypeFromFile(nsIFile* aFile, nsACSt
|
||||
nsCOMPtr<nsIMIMEInfo> info;
|
||||
|
||||
// Get the Extension
|
||||
nsCAutoString fileName;
|
||||
const char* ext = nsnull;
|
||||
rv = aFile->GetNativeLeafName(fileName);
|
||||
nsAutoString fileName;
|
||||
rv = aFile->GetLeafName(fileName);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
nsCAutoString fileExt;
|
||||
if (!fileName.IsEmpty())
|
||||
{
|
||||
PRInt32 len = fileName.Length();
|
||||
for (PRInt32 i = len; i >= 0; i--)
|
||||
{
|
||||
if (fileName[i] == '.')
|
||||
if (fileName[i] == PRUnichar('.'))
|
||||
{
|
||||
ext = fileName.get() + i + 1;
|
||||
CopyUTF16toUTF8(fileName.get() + i + 1, fileExt);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
nsDependentCString fileExt(ext ? ext : "");
|
||||
// Handle the mac case
|
||||
#if defined(XP_MAC) || defined (XP_MACOSX)
|
||||
nsCOMPtr<nsILocalFileMac> macFile;
|
||||
@ -2512,7 +2545,7 @@ NS_IMETHODIMP nsExternalHelperAppService::GetTypeFromFile(nsIFile* aFile, nsACSt
|
||||
if (icService)
|
||||
{
|
||||
rv = icService->GetMIMEInfoFromTypeCreator(type, creator, fileExt.get(), getter_AddRefs(info));
|
||||
if ( NS_SUCCEEDED( rv) )
|
||||
if (NS_SUCCEEDED(rv))
|
||||
return info->GetMIMEType(aContentType);
|
||||
}
|
||||
}
|
||||
@ -2520,7 +2553,7 @@ NS_IMETHODIMP nsExternalHelperAppService::GetTypeFromFile(nsIFile* aFile, nsACSt
|
||||
// Windows, unix and mac when no type match occured.
|
||||
if (fileExt.IsEmpty())
|
||||
return NS_ERROR_FAILURE;
|
||||
return GetTypeFromExtension( fileExt, aContentType );
|
||||
return GetTypeFromExtension(fileExt, aContentType);
|
||||
}
|
||||
|
||||
nsresult nsExternalHelperAppService::GetMIMEInfoForMimeTypeFromExtras(const nsACString& aContentType, nsIMIMEInfo * aMIMEInfo )
|
||||
|
@ -168,6 +168,7 @@ public:
|
||||
* as nsIMIMEService::GetFromTypeAndExtension.
|
||||
* This is supposed to be overridden by the platform-specific
|
||||
* nsOSHelperAppService!
|
||||
* @param aFileExt The file extension; may be empty. UTF-8 encoded.
|
||||
* @param [out] aFound
|
||||
* Should be set to PR_TRUE if the os has a mapping, to
|
||||
* PR_FALSE otherwise. Must not be null.
|
||||
|
@ -74,7 +74,7 @@ interface nsIExternalHelperAppService : nsISupports
|
||||
* is to be decoded from aEncodingType prior to saving or passing
|
||||
* off to helper apps, false otherwise.
|
||||
*/
|
||||
boolean applyDecodingForExtension(in ACString aExtension,
|
||||
boolean applyDecodingForExtension(in AUTF8String aExtension,
|
||||
in ACString aEncodingType);
|
||||
|
||||
};
|
||||
|
@ -1,4 +1,5 @@
|
||||
/* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
* vim:set ts=2 sts=2 sw=2 et cin:
|
||||
*
|
||||
* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
@ -61,8 +62,8 @@
|
||||
|
||||
// helper methods: forward declarations...
|
||||
static BYTE * GetValueBytes( HKEY hKey, const char *pValueName, DWORD *pLen=0);
|
||||
static nsresult GetExtensionFrom4xRegistryInfo(const char * aMimeType, nsCString& aFileExtension);
|
||||
static nsresult GetExtensionFromWindowsMimeDatabase(const char * aMimeType, nsCString& aFileExtension);
|
||||
static nsresult GetExtensionFrom4xRegistryInfo(const char * aMimeType, nsString& aFileExtension);
|
||||
static nsresult GetExtensionFromWindowsMimeDatabase(const char * aMimeType, nsString& aFileExtension);
|
||||
|
||||
// static member
|
||||
PRBool nsOSHelperAppService::mIsNT = PR_FALSE;
|
||||
@ -87,7 +88,7 @@ nsOSHelperAppService::~nsOSHelperAppService()
|
||||
|
||||
// The windows registry provides a mime database key which lists a set of mime types and corresponding "Extension" values.
|
||||
// we can use this to look up our mime type to see if there is a preferred extension for the mime type.
|
||||
static nsresult GetExtensionFromWindowsMimeDatabase(const char * aMimeType, nsCString& aFileExtension)
|
||||
static nsresult GetExtensionFromWindowsMimeDatabase(const char * aMimeType, nsString& aFileExtension)
|
||||
{
|
||||
HKEY hKey;
|
||||
nsCAutoString mimeDatabaseKey ("MIME\\Database\\Content Type\\");
|
||||
@ -97,14 +98,9 @@ static nsresult GetExtensionFromWindowsMimeDatabase(const char * aMimeType, nsCS
|
||||
LONG err = ::RegOpenKeyEx( HKEY_CLASSES_ROOT, mimeDatabaseKey.get(), 0, KEY_QUERY_VALUE, &hKey);
|
||||
if (err == ERROR_SUCCESS)
|
||||
{
|
||||
LPBYTE pBytes = GetValueBytes( hKey, "Extension");
|
||||
if (pBytes)
|
||||
{
|
||||
aFileExtension = (char * )pBytes;
|
||||
}
|
||||
|
||||
delete[] pBytes;
|
||||
|
||||
nsOSHelperAppService::GetValueString(hKey,
|
||||
NS_LITERAL_STRING("Extension").get(),
|
||||
aFileExtension);
|
||||
::RegCloseKey(hKey);
|
||||
}
|
||||
|
||||
@ -115,7 +111,7 @@ static nsresult GetExtensionFromWindowsMimeDatabase(const char * aMimeType, nsCS
|
||||
// helper apps based on extension. Right now, we really don't have a good place to go for
|
||||
// trying to figure out the extension for a particular mime type....One short term hack is to look
|
||||
// this information in 4.x (it's stored in the windows regsitry).
|
||||
static nsresult GetExtensionFrom4xRegistryInfo(const char * aMimeType, nsCString& aFileExtension)
|
||||
static nsresult GetExtensionFrom4xRegistryInfo(const char * aMimeType, nsString& aFileExtension)
|
||||
{
|
||||
nsCAutoString command ("Software\\Netscape\\Netscape Navigator\\Suffixes");
|
||||
nsresult rv = NS_OK;
|
||||
@ -123,21 +119,20 @@ static nsresult GetExtensionFrom4xRegistryInfo(const char * aMimeType, nsCString
|
||||
LONG err = ::RegOpenKeyEx( HKEY_CURRENT_USER, command.get(), 0, KEY_QUERY_VALUE, &hKey);
|
||||
if (err == ERROR_SUCCESS)
|
||||
{
|
||||
LPBYTE pBytes = GetValueBytes( hKey, aMimeType);
|
||||
if (pBytes) // only try to get the extension if we have a value!
|
||||
PRBool found = nsOSHelperAppService::GetValueString( hKey,
|
||||
NS_ConvertASCIItoUTF16(aMimeType).get(), aFileExtension);
|
||||
if (found) // only try to get the extension if we have a value!
|
||||
{
|
||||
aFileExtension = ".";
|
||||
aFileExtension.Append( (char *) pBytes);
|
||||
aFileExtension.Insert(PRUnichar('.'), 0);
|
||||
|
||||
// this may be a comma separate list of extensions...just take the first one
|
||||
// for now...
|
||||
|
||||
PRInt32 pos = aFileExtension.FindChar(',');
|
||||
PRInt32 pos = aFileExtension.FindChar(PRUnichar(','));
|
||||
if (pos > 0) // we have a comma separated list of languages...
|
||||
aFileExtension.Truncate(pos); // truncate everything after the first comma (including the comma)
|
||||
}
|
||||
|
||||
delete [] pBytes;
|
||||
// close the key
|
||||
::RegCloseKey(hKey);
|
||||
}
|
||||
@ -172,7 +167,7 @@ static BYTE * GetValueBytes( HKEY hKey, const char *pValueName, DWORD *pLen)
|
||||
}
|
||||
|
||||
/* static */
|
||||
PRBool nsOSHelperAppService::GetValueString(HKEY hKey, PRUnichar* pValueName, nsAString& result)
|
||||
PRBool nsOSHelperAppService::GetValueString(HKEY hKey, const PRUnichar* pValueName, nsAString& result)
|
||||
{
|
||||
if (!mIsNT) {
|
||||
nsCAutoString cValueName;
|
||||
@ -309,19 +304,31 @@ nsresult nsOSHelperAppService::GetMIMEInfoFromRegistry( const nsAFlatString& fil
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// Looks up the type for the extension aExt and compares it to aType
|
||||
static PRBool typeFromExtEquals(const char *aExt, const char *aType)
|
||||
/* static */ PRBool
|
||||
nsOSHelperAppService::typeFromExtEquals(const PRUnichar* aExt, const char *aType)
|
||||
{
|
||||
if (!aType)
|
||||
return PR_FALSE;
|
||||
nsCAutoString fileExtToUse;
|
||||
if (aExt[0] != '.')
|
||||
fileExtToUse = '.';
|
||||
nsAutoString fileExtToUse;
|
||||
if (aExt[0] != PRUnichar('.'))
|
||||
fileExtToUse = PRUnichar('.');
|
||||
|
||||
fileExtToUse.Append(aExt);
|
||||
|
||||
HKEY hKey;
|
||||
PRBool eq = PR_FALSE;
|
||||
LONG err = ::RegOpenKeyEx( HKEY_CLASSES_ROOT, fileExtToUse.get(), 0, KEY_QUERY_VALUE, &hKey);
|
||||
LONG err;
|
||||
if (mIsNT) {
|
||||
err = ::RegOpenKeyExW(HKEY_CLASSES_ROOT, fileExtToUse.get(), 0, KEY_QUERY_VALUE, &hKey);
|
||||
}
|
||||
else {
|
||||
nsCAutoString ansiKey;
|
||||
nsresult rv = NS_CopyUnicodeToNative(fileExtToUse, ansiKey);
|
||||
if (NS_FAILED(rv))
|
||||
return eq;
|
||||
|
||||
err = ::RegOpenKeyEx( HKEY_CLASSES_ROOT, ansiKey.get(), 0, KEY_QUERY_VALUE, &hKey);
|
||||
}
|
||||
if (err == ERROR_SUCCESS)
|
||||
{
|
||||
LPBYTE pBytes = GetValueBytes(hKey, "Content Type");
|
||||
@ -335,22 +342,34 @@ static PRBool typeFromExtEquals(const char *aExt, const char *aType)
|
||||
return eq;
|
||||
}
|
||||
|
||||
already_AddRefed<nsMIMEInfoWin> nsOSHelperAppService::GetByExtension(const char *aFileExt, const char *aTypeHint)
|
||||
already_AddRefed<nsMIMEInfoWin> nsOSHelperAppService::GetByExtension(const nsAFlatString& aFileExt, const char *aTypeHint)
|
||||
{
|
||||
if (!aFileExt || !*aFileExt)
|
||||
if (aFileExt.IsEmpty())
|
||||
return nsnull;
|
||||
|
||||
// windows registry assumes your file extension is going to include the '.'.
|
||||
// so make sure it's there...
|
||||
nsCAutoString fileExtToUse;
|
||||
if (*aFileExt != '.')
|
||||
fileExtToUse = '.';
|
||||
nsAutoString fileExtToUse;
|
||||
if (aFileExt.First() != PRUnichar('.'))
|
||||
fileExtToUse = PRUnichar('.');
|
||||
|
||||
fileExtToUse.Append(aFileExt);
|
||||
|
||||
// o.t. try to get an entry from the windows registry.
|
||||
HKEY hKey;
|
||||
LONG err = ::RegOpenKeyEx( HKEY_CLASSES_ROOT, fileExtToUse.get(), 0, KEY_QUERY_VALUE, &hKey);
|
||||
LONG err;
|
||||
if (mIsNT) {
|
||||
err = ::RegOpenKeyExW(HKEY_CLASSES_ROOT, fileExtToUse.get(), 0, KEY_QUERY_VALUE, &hKey);
|
||||
}
|
||||
else {
|
||||
nsCAutoString ansiKey;
|
||||
nsresult rv = NS_CopyUnicodeToNative(fileExtToUse, ansiKey);
|
||||
if (NS_FAILED(rv))
|
||||
return nsnull;
|
||||
|
||||
err = ::RegOpenKeyEx( HKEY_CLASSES_ROOT, ansiKey.get(), 0, KEY_QUERY_VALUE, &hKey);
|
||||
}
|
||||
|
||||
if (err == ERROR_SUCCESS)
|
||||
{
|
||||
nsCAutoString typeToUse;
|
||||
@ -371,7 +390,7 @@ already_AddRefed<nsMIMEInfoWin> nsOSHelperAppService::GetByExtension(const char
|
||||
{
|
||||
NS_ADDREF(mimeInfo);
|
||||
// don't append the '.'
|
||||
mimeInfo->AppendExtension(Substring(fileExtToUse, 1));
|
||||
mimeInfo->AppendExtension(NS_ConvertUTF16toUTF8(Substring(fileExtToUse, 1)));
|
||||
|
||||
mimeInfo->SetPreferredAction(nsIMIMEInfo::useSystemDefault);
|
||||
|
||||
@ -411,7 +430,7 @@ already_AddRefed<nsIMIMEInfo> nsOSHelperAppService::GetMIMEInfoFromOS(const nsAC
|
||||
const nsCString& flatType = PromiseFlatCString(aMIMEType);
|
||||
const nsCString& flatExt = PromiseFlatCString(aFileExt);
|
||||
|
||||
nsCAutoString fileExtension;
|
||||
nsAutoString fileExtension;
|
||||
/* XXX The Equals is a gross hack to wallpaper over the most common Win32
|
||||
* extension issues caused by the fix for bug 116938. See bug
|
||||
* 120327, comment 271 for why this is needed. Not even sure we
|
||||
@ -420,7 +439,7 @@ already_AddRefed<nsIMIMEInfo> nsOSHelperAppService::GetMIMEInfoFromOS(const nsAC
|
||||
* useless....
|
||||
* We'll do extension-based lookup for this type later in this function.
|
||||
*/
|
||||
if (!aMIMEType.Equals(APPLICATION_OCTET_STREAM, nsCaseInsensitiveCStringComparator())) {
|
||||
if (!aMIMEType.LowerCaseEqualsLiteral(APPLICATION_OCTET_STREAM)) {
|
||||
// (1) try to use the windows mime database to see if there is a mapping to a file extension
|
||||
// (2) try to see if we have some left over 4.x registry info we can peek at...
|
||||
GetExtensionFromWindowsMimeDatabase(flatType.get(), fileExtension);
|
||||
@ -433,7 +452,7 @@ already_AddRefed<nsIMIMEInfo> nsOSHelperAppService::GetMIMEInfoFromOS(const nsAC
|
||||
// If we found an extension for the type, do the lookup
|
||||
nsMIMEInfoWin* mi = nsnull;
|
||||
if (!fileExtension.IsEmpty())
|
||||
mi = GetByExtension(fileExtension.get(), flatType.get()).get();
|
||||
mi = GetByExtension(fileExtension, flatType.get()).get();
|
||||
LOG(("Extension lookup on '%s' found: 0x%p\n", fileExtension.get(), mi));
|
||||
|
||||
PRBool hasDefault = PR_FALSE;
|
||||
@ -444,7 +463,7 @@ already_AddRefed<nsIMIMEInfo> nsOSHelperAppService::GetMIMEInfoFromOS(const nsAC
|
||||
// to the mimeinfo that we have. (E.g.: We are asked for video/mpeg and
|
||||
// .mpg, but the primary extension for video/mpeg is .mpeg. But because
|
||||
// .mpg is an extension for video/mpeg content, we want to append it)
|
||||
if (!aFileExt.IsEmpty() && typeFromExtEquals(flatExt.get(), flatType.get())) {
|
||||
if (!aFileExt.IsEmpty() && typeFromExtEquals(NS_ConvertUTF8toUTF16(flatExt).get(), flatType.get())) {
|
||||
LOG(("Appending extension '%s' to mimeinfo, because its mimetype is '%s'\n",
|
||||
flatExt.get(), flatType.get()));
|
||||
PRBool extExist = PR_FALSE;
|
||||
@ -454,7 +473,8 @@ already_AddRefed<nsIMIMEInfo> nsOSHelperAppService::GetMIMEInfoFromOS(const nsAC
|
||||
}
|
||||
}
|
||||
if (!mi || !hasDefault) {
|
||||
nsRefPtr<nsMIMEInfoWin> miByExt = GetByExtension(flatExt.get(), flatType.get());
|
||||
nsRefPtr<nsMIMEInfoWin> miByExt =
|
||||
GetByExtension(NS_ConvertUTF8toUTF16(aFileExt), flatType.get());
|
||||
LOG(("Ext. lookup for '%s' found 0x%p\n", flatExt.get(), miByExt.get()));
|
||||
if (!miByExt && mi)
|
||||
return mi;
|
||||
|
@ -64,18 +64,21 @@ public:
|
||||
// method overrides for windows registry look up steps....
|
||||
already_AddRefed<nsIMIMEInfo> GetMIMEInfoFromOS(const nsACString& aMIMEType, const nsACString& aFileExt, PRBool *aFound);
|
||||
|
||||
/** Get the string value of a registry value and store it in result.
|
||||
* @return PR_TRUE on success, PR_FALSE on failure
|
||||
*/
|
||||
static PRBool GetValueString(HKEY hKey, const PRUnichar* pValueName, nsAString& result);
|
||||
|
||||
protected:
|
||||
// Lookup a mime info by extension, using an optional type hint
|
||||
already_AddRefed<nsMIMEInfoWin> GetByExtension(const char *aFileExt, const char *aTypeHint = nsnull);
|
||||
already_AddRefed<nsMIMEInfoWin> GetByExtension(const nsAFlatString& aFileExt, const char *aTypeHint = nsnull);
|
||||
nsresult FindOSMimeInfoForType(const char * aMimeContentType, nsIURI * aURI, char ** aFileExtension, nsIMIMEInfo ** aMIMEInfo);
|
||||
|
||||
/** Whether we're running on an OS that supports the *W registry functions */
|
||||
static PRBool mIsNT;
|
||||
/** Get the string value of a registry value and store it in result.
|
||||
* @return PR_TRUE on success, PR_FALSE on failure
|
||||
*/
|
||||
static PRBool GetValueString(HKEY hKey, PRUnichar* pValueName, nsAString& result);
|
||||
static nsresult GetMIMEInfoFromRegistry(const nsAFlatString& fileType, nsIMIMEInfo *pInfo);
|
||||
/// Looks up the type for the extension aExt and compares it to aType
|
||||
static PRBool typeFromExtEquals(const PRUnichar* aExt, const char *aType);
|
||||
};
|
||||
|
||||
#endif // nsOSHelperAppService_h__
|
||||
|
Loading…
Reference in New Issue
Block a user