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

Commit

Permalink
Bug 1352348 - add a method to nsExternalHelperAppService that only fe…
Browse files Browse the repository at this point in the history
…tches a string mimetype for an extension, r=bz,Paolo

MozReview-Commit-ID: 3p1pakC9g45
  • Loading branch information
gijsk committed Apr 13, 2017
1 parent c8875b7 commit e6df220
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 38 deletions.
16 changes: 11 additions & 5 deletions uriloader/exthandler/nsExternalHelperAppService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2729,14 +2729,12 @@ nsExternalHelperAppService::GetTypeFromExtension(const nsACString& aFileExt,
}

// Ask OS.
bool found = false;
nsCOMPtr<nsIMIMEInfo> mi = GetMIMEInfoFromOS(EmptyCString(), aFileExt, &found);
if (mi && found) {
return mi->GetMIMEType(aContentType);
if (GetMIMETypeFromOSForExtension(aFileExt, aContentType)) {
return NS_OK;
}

// Check extras array.
found = GetTypeFromExtras(aFileExt, aContentType);
bool found = GetTypeFromExtras(aFileExt, aContentType);
if (found) {
return NS_OK;
}
Expand Down Expand Up @@ -2935,3 +2933,11 @@ bool nsExternalHelperAppService::GetTypeFromExtras(const nsACString& aExtension,

return false;
}

bool
nsExternalHelperAppService::GetMIMETypeFromOSForExtension(const nsACString& aExtension, nsACString& aMIMEType)
{
bool found = false;
nsCOMPtr<nsIMIMEInfo> mimeInfo = GetMIMEInfoFromOS(EmptyCString(), aExtension, &found);
return found && mimeInfo && NS_SUCCEEDED(mimeInfo->GetMIMEType(aMIMEType));
}
9 changes: 9 additions & 0 deletions uriloader/exthandler/nsExternalHelperAppService.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ class nsExternalHelperAppService
virtual nsresult OSProtocolHandlerExists(const char *aScheme,
bool *aExists) = 0;

/**
* Given an extension, get a MIME type string. If not overridden by
* the OS-specific nsOSHelperAppService, will call into GetMIMEInfoFromOS
* with an empty mimetype.
* @return true if we successfully found a mimetype.
*/
virtual bool GetMIMETypeFromOSForExtension(const nsACString& aExtension,
nsACString& aMIMEType);

protected:
virtual ~nsExternalHelperAppService();

Expand Down
94 changes: 61 additions & 33 deletions uriloader/exthandler/win/nsOSHelperAppService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,46 +392,28 @@ nsOSHelperAppService::GetDefaultAppInfo(const nsAString& aAppInfo,

already_AddRefed<nsMIMEInfoWin> nsOSHelperAppService::GetByExtension(const nsAFlatString& aFileExt, const char *aTypeHint)
{
if (aFileExt.IsEmpty())
if (aExtension.IsEmpty())
return nullptr;

// windows registry assumes your file extension is going to include the '.'.
// so make sure it's there...
nsAutoString fileExtToUse;
if (aFileExt.First() != char16_t('.'))
fileExtToUse = char16_t('.');

fileExtToUse.Append(aFileExt);

// Try to get an entry from the windows registry.
nsCOMPtr<nsIWindowsRegKey> regKey =
do_CreateInstance("@mozilla.org/windows-registry-key;1");
if (!regKey)
return nullptr;

nsresult rv = regKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT,
fileExtToUse,
nsIWindowsRegKey::ACCESS_QUERY_VALUE);
if (NS_FAILED(rv))
return nullptr;

// Determine the mime type.
nsAutoCString typeToUse;
if (aTypeHint && *aTypeHint) {
typeToUse.Assign(aTypeHint);
}
else {
nsAutoString temp;
if (NS_FAILED(regKey->ReadStringValue(NS_LITERAL_STRING("Content Type"),
temp)) || temp.IsEmpty()) {
return nullptr;
}
// Content-Type is always in ASCII
LossyAppendUTF16toASCII(temp, typeToUse);
} else if (!GetMIMETypeFromOSForExtension(NS_ConvertUTF16toUTF8(aFileExt), typeToUse)) {
return nullptr;
}

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

// don't append the '.'
// windows registry assumes your file extension is going to include the '.',
// but our APIs expect it to not be there, so make sure we normalize that bit.
nsAutoString fileExtToUse;
if (aFileExt.First() != char16_t('.'))
fileExtToUse = char16_t('.');

fileExtToUse.Append(aFileExt);

// don't append the '.' for our APIs.
mimeInfo->AppendExtension(NS_ConvertUTF16toUTF8(Substring(fileExtToUse, 1)));
mimeInfo->SetPreferredAction(nsIMIMEInfo::useSystemDefault);

Expand All @@ -458,8 +440,17 @@ already_AddRefed<nsMIMEInfoWin> nsOSHelperAppService::GetByExtension(const nsAFl
}
else
{
found = NS_SUCCEEDED(regKey->ReadStringValue(EmptyString(),
appInfo));
nsCOMPtr<nsIWindowsRegKey> regKey =
do_CreateInstance("@mozilla.org/windows-registry-key;1");
if (!regKey)
return nullptr;
nsresult rv = regKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT,
fileExtToUse,
nsIWindowsRegKey::ACCESS_QUERY_VALUE);
if (NS_SUCCEEDED(rv)) {
found = NS_SUCCEEDED(regKey->ReadStringValue(EmptyString(),
appInfo));
}
}

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

bool
nsOSHelperAppService::GetMIMETypeFromOSForExtension(const nsACString& aExtension,
nsACString& aMIMEType)
{
if (aExtension.IsEmpty())
return false;

// windows registry assumes your file extension is going to include the '.'.
// so make sure it's there...
nsAutoString fileExtToUse;
if (aExtension.First() != '.')
fileExtToUse = char16_t('.');

AppendUTF8toUTF16(aExtension, fileExtToUse);

// Try to get an entry from the windows registry.
nsCOMPtr<nsIWindowsRegKey> regKey =
do_CreateInstance("@mozilla.org/windows-registry-key;1");
if (!regKey)
return false;

nsresult rv = regKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT,
fileExtToUse,
nsIWindowsRegKey::ACCESS_QUERY_VALUE);
if (NS_FAILED(rv))
return false;

nsAutoString mimeType;
if (NS_FAILED(regKey->ReadStringValue(NS_LITERAL_STRING("Content Type"),
mimeType)) || mimeType.IsEmpty()) {
return false;
}
// Content-Type is always in ASCII
aMIMEType.Truncate();
LossyAppendUTF16toASCII(mimeType, aMIMEType);
return true;
}
2 changes: 2 additions & 0 deletions uriloader/exthandler/win/nsOSHelperAppService.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class nsOSHelperAppService : public nsExternalHelperAppService
NS_IMETHOD GetProtocolHandlerInfoFromOS(const nsACString &aScheme,
bool *found,
nsIHandlerInfo **_retval);
virtual bool GetMIMETypeFromOSForExtension(const nsACString& aExtension,
nsACString& aMIMEType) override;

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

0 comments on commit e6df220

Please sign in to comment.