Skip to content
This repository was archived by the owner on Aug 4, 2022. It is now read-only.

Commit e6df220

Browse files
committed
Bug 1352348 - add a method to nsExternalHelperAppService that only fetches a string mimetype for an extension, r=bz,Paolo
MozReview-Commit-ID: 3p1pakC9g45
1 parent c8875b7 commit e6df220

File tree

4 files changed

+83
-38
lines changed

4 files changed

+83
-38
lines changed

uriloader/exthandler/nsExternalHelperAppService.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2729,14 +2729,12 @@ nsExternalHelperAppService::GetTypeFromExtension(const nsACString& aFileExt,
27292729
}
27302730

27312731
// Ask OS.
2732-
bool found = false;
2733-
nsCOMPtr<nsIMIMEInfo> mi = GetMIMEInfoFromOS(EmptyCString(), aFileExt, &found);
2734-
if (mi && found) {
2735-
return mi->GetMIMEType(aContentType);
2732+
if (GetMIMETypeFromOSForExtension(aFileExt, aContentType)) {
2733+
return NS_OK;
27362734
}
27372735

27382736
// Check extras array.
2739-
found = GetTypeFromExtras(aFileExt, aContentType);
2737+
bool found = GetTypeFromExtras(aFileExt, aContentType);
27402738
if (found) {
27412739
return NS_OK;
27422740
}
@@ -2935,3 +2933,11 @@ bool nsExternalHelperAppService::GetTypeFromExtras(const nsACString& aExtension,
29352933

29362934
return false;
29372935
}
2936+
2937+
bool
2938+
nsExternalHelperAppService::GetMIMETypeFromOSForExtension(const nsACString& aExtension, nsACString& aMIMEType)
2939+
{
2940+
bool found = false;
2941+
nsCOMPtr<nsIMIMEInfo> mimeInfo = GetMIMEInfoFromOS(EmptyCString(), aExtension, &found);
2942+
return found && mimeInfo && NS_SUCCEEDED(mimeInfo->GetMIMEType(aMIMEType));
2943+
}

uriloader/exthandler/nsExternalHelperAppService.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ class nsExternalHelperAppService
108108
virtual nsresult OSProtocolHandlerExists(const char *aScheme,
109109
bool *aExists) = 0;
110110

111+
/**
112+
* Given an extension, get a MIME type string. If not overridden by
113+
* the OS-specific nsOSHelperAppService, will call into GetMIMEInfoFromOS
114+
* with an empty mimetype.
115+
* @return true if we successfully found a mimetype.
116+
*/
117+
virtual bool GetMIMETypeFromOSForExtension(const nsACString& aExtension,
118+
nsACString& aMIMEType);
119+
111120
protected:
112121
virtual ~nsExternalHelperAppService();
113122

uriloader/exthandler/win/nsOSHelperAppService.cpp

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -392,46 +392,28 @@ nsOSHelperAppService::GetDefaultAppInfo(const nsAString& aAppInfo,
392392

393393
already_AddRefed<nsMIMEInfoWin> nsOSHelperAppService::GetByExtension(const nsAFlatString& aFileExt, const char *aTypeHint)
394394
{
395-
if (aFileExt.IsEmpty())
395+
if (aExtension.IsEmpty())
396396
return nullptr;
397397

398-
// windows registry assumes your file extension is going to include the '.'.
399-
// so make sure it's there...
400-
nsAutoString fileExtToUse;
401-
if (aFileExt.First() != char16_t('.'))
402-
fileExtToUse = char16_t('.');
403-
404-
fileExtToUse.Append(aFileExt);
405-
406-
// Try to get an entry from the windows registry.
407-
nsCOMPtr<nsIWindowsRegKey> regKey =
408-
do_CreateInstance("@mozilla.org/windows-registry-key;1");
409-
if (!regKey)
410-
return nullptr;
411-
412-
nsresult rv = regKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT,
413-
fileExtToUse,
414-
nsIWindowsRegKey::ACCESS_QUERY_VALUE);
415-
if (NS_FAILED(rv))
416-
return nullptr;
417-
398+
// Determine the mime type.
418399
nsAutoCString typeToUse;
419400
if (aTypeHint && *aTypeHint) {
420401
typeToUse.Assign(aTypeHint);
421-
}
422-
else {
423-
nsAutoString temp;
424-
if (NS_FAILED(regKey->ReadStringValue(NS_LITERAL_STRING("Content Type"),
425-
temp)) || temp.IsEmpty()) {
426-
return nullptr;
427-
}
428-
// Content-Type is always in ASCII
429-
LossyAppendUTF16toASCII(temp, typeToUse);
402+
} else if (!GetMIMETypeFromOSForExtension(NS_ConvertUTF16toUTF8(aFileExt), typeToUse)) {
403+
return nullptr;
430404
}
431405

432406
RefPtr<nsMIMEInfoWin> mimeInfo = new nsMIMEInfoWin(typeToUse);
433407

434-
// don't append the '.'
408+
// windows registry assumes your file extension is going to include the '.',
409+
// but our APIs expect it to not be there, so make sure we normalize that bit.
410+
nsAutoString fileExtToUse;
411+
if (aFileExt.First() != char16_t('.'))
412+
fileExtToUse = char16_t('.');
413+
414+
fileExtToUse.Append(aFileExt);
415+
416+
// don't append the '.' for our APIs.
435417
mimeInfo->AppendExtension(NS_ConvertUTF16toUTF8(Substring(fileExtToUse, 1)));
436418
mimeInfo->SetPreferredAction(nsIMIMEInfo::useSystemDefault);
437419

@@ -458,8 +440,17 @@ already_AddRefed<nsMIMEInfoWin> nsOSHelperAppService::GetByExtension(const nsAFl
458440
}
459441
else
460442
{
461-
found = NS_SUCCEEDED(regKey->ReadStringValue(EmptyString(),
462-
appInfo));
443+
nsCOMPtr<nsIWindowsRegKey> regKey =
444+
do_CreateInstance("@mozilla.org/windows-registry-key;1");
445+
if (!regKey)
446+
return nullptr;
447+
nsresult rv = regKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT,
448+
fileExtToUse,
449+
nsIWindowsRegKey::ACCESS_QUERY_VALUE);
450+
if (NS_SUCCEEDED(rv)) {
451+
found = NS_SUCCEEDED(regKey->ReadStringValue(EmptyString(),
452+
appInfo));
453+
}
463454
}
464455

465456
// Bug 358297 - ignore the default handler, force the user to choose app
@@ -597,3 +588,40 @@ nsOSHelperAppService::GetProtocolHandlerInfoFromOS(const nsACString &aScheme,
597588
return NS_OK;
598589
}
599590

591+
bool
592+
nsOSHelperAppService::GetMIMETypeFromOSForExtension(const nsACString& aExtension,
593+
nsACString& aMIMEType)
594+
{
595+
if (aExtension.IsEmpty())
596+
return false;
597+
598+
// windows registry assumes your file extension is going to include the '.'.
599+
// so make sure it's there...
600+
nsAutoString fileExtToUse;
601+
if (aExtension.First() != '.')
602+
fileExtToUse = char16_t('.');
603+
604+
AppendUTF8toUTF16(aExtension, fileExtToUse);
605+
606+
// Try to get an entry from the windows registry.
607+
nsCOMPtr<nsIWindowsRegKey> regKey =
608+
do_CreateInstance("@mozilla.org/windows-registry-key;1");
609+
if (!regKey)
610+
return false;
611+
612+
nsresult rv = regKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT,
613+
fileExtToUse,
614+
nsIWindowsRegKey::ACCESS_QUERY_VALUE);
615+
if (NS_FAILED(rv))
616+
return false;
617+
618+
nsAutoString mimeType;
619+
if (NS_FAILED(regKey->ReadStringValue(NS_LITERAL_STRING("Content Type"),
620+
mimeType)) || mimeType.IsEmpty()) {
621+
return false;
622+
}
623+
// Content-Type is always in ASCII
624+
aMIMEType.Truncate();
625+
LossyAppendUTF16toASCII(mimeType, aMIMEType);
626+
return true;
627+
}

uriloader/exthandler/win/nsOSHelperAppService.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class nsOSHelperAppService : public nsExternalHelperAppService
4040
NS_IMETHOD GetProtocolHandlerInfoFromOS(const nsACString &aScheme,
4141
bool *found,
4242
nsIHandlerInfo **_retval);
43+
virtual bool GetMIMETypeFromOSForExtension(const nsACString& aExtension,
44+
nsACString& aMIMEType) override;
4345

4446
/** Get the string value of a registry value and store it in result.
4547
* @return true on success, false on failure

0 commit comments

Comments
 (0)