From aa7849b37272116120492790dc30480848714387 Mon Sep 17 00:00:00 2001 From: Roy Tam Date: Sun, 24 Mar 2019 07:24:27 +0800 Subject: [PATCH] restore pre-vista logic that is deleted in https://github.com/MoonchildProductions/UXP/commit/3c02dbad946487a83783a1923463a1ebb96aae19 --- .../exthandler/win/nsOSHelperAppService.cpp | 144 +++++++++++------- 1 file changed, 88 insertions(+), 56 deletions(-) diff --git a/uriloader/exthandler/win/nsOSHelperAppService.cpp b/uriloader/exthandler/win/nsOSHelperAppService.cpp index 48b6f1795d..5d7120df4c 100644 --- a/uriloader/exthandler/win/nsOSHelperAppService.cpp +++ b/uriloader/exthandler/win/nsOSHelperAppService.cpp @@ -81,39 +81,34 @@ nsresult nsOSHelperAppService::OSProtocolHandlerExists(const char * aProtocolSch *aHandlerExists = false; if (aProtocolScheme && *aProtocolScheme) { - NS_ENSURE_TRUE(mAppAssoc, NS_ERROR_NOT_AVAILABLE); - wchar_t * pResult = nullptr; - NS_ConvertASCIItoUTF16 scheme(aProtocolScheme); - // We are responsible for freeing returned strings. - HRESULT hr = mAppAssoc->QueryCurrentDefault(scheme.get(), - AT_URLPROTOCOL, AL_EFFECTIVE, - &pResult); - if (SUCCEEDED(hr)) { - CoTaskMemFree(pResult); - // Check the registry to see if it's a valid handler. - nsCOMPtr regKey = do_CreateInstance("@mozilla.org/windows-registry-key;1"); - if (!regKey) { - return NS_ERROR_NOT_AVAILABLE; + // Vista: use new application association interface + if (mAppAssoc) { + wchar_t * pResult = nullptr; + NS_ConvertASCIItoUTF16 scheme(aProtocolScheme); + // We are responsible for freeing returned strings. + HRESULT hr = mAppAssoc->QueryCurrentDefault(scheme.get(), + AT_URLPROTOCOL, AL_EFFECTIVE, + &pResult); + if (SUCCEEDED(hr)) { + CoTaskMemFree(pResult); + *aHandlerExists = true; } + return NS_OK; + } - nsresult rv = regKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT, - nsDependentString(scheme.get()), - nsIWindowsRegKey::ACCESS_QUERY_VALUE); - if (NS_FAILED(rv)) { - // Open will fail if the registry key path doesn't exist. - return NS_OK; - } - - bool hasValue; - rv = regKey->HasValue(NS_LITERAL_STRING("URL Protocol"), &hasValue); - if (NS_FAILED(rv)) { - return NS_ERROR_FAILURE; - } - if (!hasValue) { - return NS_OK; - } - - *aHandlerExists = true; + HKEY hKey; + LONG err = ::RegOpenKeyExW(HKEY_CLASSES_ROOT, + NS_ConvertASCIItoUTF16(aProtocolScheme).get(), + 0, + KEY_QUERY_VALUE, + &hKey); + if (err == ERROR_SUCCESS) + { + err = ::RegQueryValueExW(hKey, L"URL Protocol", + nullptr, nullptr, nullptr, nullptr); + *aHandlerExists = (err == ERROR_SUCCESS); + // close the key + ::RegCloseKey(hKey); } } @@ -144,21 +139,40 @@ NS_IMETHODIMP nsOSHelperAppService::GetApplicationDescription(const nsACString& } } - NS_ENSURE_TRUE(mAppAssoc, NS_ERROR_NOT_AVAILABLE); - wchar_t * pResult = nullptr; - // We are responsible for freeing returned strings. - HRESULT hr = mAppAssoc->QueryCurrentDefault(buf.get(), - AT_URLPROTOCOL, AL_EFFECTIVE, - &pResult); - if (SUCCEEDED(hr)) { - nsCOMPtr app; - nsAutoString appInfo(pResult); - CoTaskMemFree(pResult); - if (NS_SUCCEEDED(GetDefaultAppInfo(appInfo, _retval, getter_AddRefs(app)))) - return NS_OK; + if (mAppAssoc) { + // Vista: use new application association interface + wchar_t * pResult = nullptr; + // We are responsible for freeing returned strings. + HRESULT hr = mAppAssoc->QueryCurrentDefault(buf.get(), + AT_URLPROTOCOL, AL_EFFECTIVE, + &pResult); + if (SUCCEEDED(hr)) { + nsCOMPtr app; + nsAutoString appInfo(pResult); + CoTaskMemFree(pResult); + if (NS_SUCCEEDED(GetDefaultAppInfo(appInfo, _retval, getter_AddRefs(app)))) + return NS_OK; + } + return NS_ERROR_NOT_AVAILABLE; } - return NS_ERROR_NOT_AVAILABLE; + nsCOMPtr app; + GetDefaultAppInfo(buf, _retval, getter_AddRefs(app)); + + if (!_retval.Equals(buf)) + return NS_OK; + + // Fall back to full path + buf.AppendLiteral("\\shell\\open\\command"); + nsresult rv = regKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT, + buf, + nsIWindowsRegKey::ACCESS_QUERY_VALUE); + if (NS_FAILED(rv)) + return NS_ERROR_NOT_AVAILABLE; + + rv = regKey->ReadStringValue(EmptyString(), _retval); + + return NS_SUCCEEDED(rv) ? NS_OK : NS_ERROR_NOT_AVAILABLE; } // GetMIMEInfoFromRegistry: This function obtains the values of some of the nsIMIMEInfo @@ -366,18 +380,36 @@ already_AddRefed nsOSHelperAppService::GetByExtension(const nsAFl bool found; // Retrieve the default application for this extension - NS_ENSURE_TRUE(mAppAssoc, nullptr); - nsString assocType(fileExtToUse); - wchar_t * pResult = nullptr; - HRESULT hr = mAppAssoc->QueryCurrentDefault(assocType.get(), - AT_FILEEXTENSION, AL_EFFECTIVE, - &pResult); - if (SUCCEEDED(hr)) { - found = true; - appInfo.Assign(pResult); - CoTaskMemFree(pResult); - } else { - found = false; + if (mAppAssoc) { + // Vista: use the new application association COM interfaces + // for resolving helpers. + nsString assocType(fileExtToUse); + wchar_t * pResult = nullptr; + HRESULT hr = mAppAssoc->QueryCurrentDefault(assocType.get(), + AT_FILEEXTENSION, AL_EFFECTIVE, + &pResult); + if (SUCCEEDED(hr)) { + found = true; + appInfo.Assign(pResult); + CoTaskMemFree(pResult); + } + else { + found = false; + } + } + else + { + nsCOMPtr 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