Bug 1571247 - don't default to the jfif extension for image/jpeg files as suggested by the Windows registry, r=agashlin

Differential Revision: https://phabricator.services.mozilla.com/D87371
This commit is contained in:
Gijs Kruitbosch 2020-08-21 15:38:35 +00:00
Родитель e5506d0ac0
Коммит c1e5ec87eb
2 изменённых файлов: 46 добавлений и 7 удалений

Просмотреть файл

@ -487,7 +487,7 @@ static const nsExtraMimeTypeEntry extraMimeEntries[] = {
{IMAGE_BMP, "bmp", "BMP Image"},
{IMAGE_GIF, "gif", "GIF Image"},
{IMAGE_ICO, "ico,cur", "ICO Image"},
{IMAGE_JPEG, "jpeg,jpg,jfif,pjpeg,pjp", "JPEG Image"},
{IMAGE_JPEG, "jpg,jpeg,jfif,pjpeg,pjp", "JPEG Image"},
{IMAGE_PNG, "png", "PNG Image"},
{IMAGE_APNG, "apng", "APNG Image"},
{IMAGE_TIFF, "tiff,tif", "TIFF Image"},
@ -528,6 +528,9 @@ static const nsExtraMimeTypeEntry extraMimeEntries[] = {
{AUDIO_MIDI, "mid", "Standard MIDI Audio"},
{APPLICATION_WASM, "wasm", "WebAssembly Module"}};
static const nsDefaultMimeTypeEntry sForbiddenPrimaryExtensions[] = {
{IMAGE_JPEG, "jfif"}};
/**
* File extensions for which decoding should be disabled.
* NOTE: These MUST be lower-case and ASCII.
@ -2631,6 +2634,17 @@ NS_IMETHODIMP nsExternalHelperAppService::GetFromTypeAndExtension(
LOG(("Falling back to 'File' file description\n"));
}
// Sometimes, OSes give us bad data. We have a set of forbidden extensions
// for some MIME types. If the primary extension is forbidden,
// overwrite it with a known-good one. See bug 1571247 for context.
nsAutoCString primaryExtension;
(*_retval)->GetPrimaryExtension(primaryExtension);
if (!primaryExtension.EqualsIgnoreCase(PromiseFlatCString(aFileExt).get())) {
if (MaybeReplacePrimaryExtension(primaryExtension, *_retval)) {
(*_retval)->GetPrimaryExtension(primaryExtension);
}
}
// Finally, check if we got a file extension and if yes, if it is an
// extension on the mimeinfo, in which case we want it to be the primary one
if (!aFileExt.IsEmpty()) {
@ -2642,6 +2656,7 @@ NS_IMETHODIMP nsExternalHelperAppService::GetFromTypeAndExtension(
nsAutoCString fileExt;
ToLowerCase(aFileExt, fileExt);
(*_retval)->SetPrimaryExtension(fileExt);
primaryExtension = fileExt;
}
}
@ -2649,9 +2664,7 @@ NS_IMETHODIMP nsExternalHelperAppService::GetFromTypeAndExtension(
// type is in our list; these are file formats supported by Firefox and
// we don't want other brands positioning themselves as the sole viewer
// for a system.
nsAutoCString primaryExtension;
rv = (*_retval)->GetPrimaryExtension(primaryExtension);
if (NS_SUCCEEDED(rv)) {
if (!primaryExtension.IsEmpty()) {
for (const char* ext : descriptionOverwriteExtensions) {
if (primaryExtension.Equals(ext)) {
nsCOMPtr<nsIStringBundleService> bundleService =
@ -2680,10 +2693,8 @@ NS_IMETHODIMP nsExternalHelperAppService::GetFromTypeAndExtension(
nsAutoCString type;
(*_retval)->GetMIMEType(type);
nsAutoCString ext;
(*_retval)->GetPrimaryExtension(ext);
LOG(("MIME Info Summary: Type '%s', Primary Ext '%s'\n", type.get(),
ext.get()));
primaryExtension.get()));
}
return NS_OK;
@ -2890,6 +2901,26 @@ nsresult nsExternalHelperAppService::FillMIMEInfoForExtensionFromExtras(
return FillMIMEInfoForMimeTypeFromExtras(type, true, aMIMEInfo);
}
bool nsExternalHelperAppService::MaybeReplacePrimaryExtension(
const nsACString& aPrimaryExtension, nsIMIMEInfo* aMIMEInfo) {
for (const auto& entry : sForbiddenPrimaryExtensions) {
if (aPrimaryExtension.LowerCaseEqualsASCII(entry.mFileExtension)) {
nsDependentCString mime(entry.mMimeType);
for (const auto& extraEntry : extraMimeEntries) {
if (mime.LowerCaseEqualsASCII(extraEntry.mMimeType)) {
nsDependentCString goodExts(extraEntry.mFileExtensions);
int32_t commaPos = goodExts.FindChar(',');
commaPos = commaPos == kNotFound ? goodExts.Length() : commaPos;
auto goodExt = Substring(goodExts, 0, commaPos);
aMIMEInfo->SetPrimaryExtension(goodExt);
return true;
}
}
}
}
return false;
}
bool nsExternalHelperAppService::GetTypeFromExtras(const nsACString& aExtension,
nsACString& aMIMEType) {
NS_ASSERTION(!aExtension.IsEmpty(), "Empty aExtension parameter!");

Просмотреть файл

@ -143,6 +143,14 @@ class nsExternalHelperAppService : public nsIExternalHelperAppService,
nsresult FillMIMEInfoForExtensionFromExtras(const nsACString& aExtension,
nsIMIMEInfo* aMIMEInfo);
/**
* Replace the primary extension of the mimeinfo object if it's in our
* list of forbidden extensions. This fixes up broken information
* provided to us by the OS.
*/
bool MaybeReplacePrimaryExtension(const nsACString& aPrimaryExtension,
nsIMIMEInfo* aMIMEInfo);
/**
* Searches the "extra" array for a MIME type, and gets its extension.
* @param aExtension The extension to search for