From c7668971968e044e85446d79362d7744846efdd0 Mon Sep 17 00:00:00 2001 From: wolfbeast Date: Mon, 19 Feb 2018 10:00:25 +0100 Subject: Remove the use of GetProcAddress() for shell32 This avoids manually hooking into shell32.dll and using the native shell API instead. Tag #22. --- image/decoders/icon/win/nsIconChannel.cpp | 53 +++++++++------------------ toolkit/components/alerts/nsAlertsService.cpp | 23 ++++-------- toolkit/components/alerts/nsAlertsService.h | 17 --------- toolkit/xre/nsEmbedFunctions.cpp | 24 +----------- widget/windows/WinTaskbar.cpp | 47 ++---------------------- 5 files changed, 31 insertions(+), 133 deletions(-) diff --git a/image/decoders/icon/win/nsIconChannel.cpp b/image/decoders/icon/win/nsIconChannel.cpp index 9ddcbbc488..04680627ae 100644 --- a/image/decoders/icon/win/nsIconChannel.cpp +++ b/image/decoders/icon/win/nsIconChannel.cpp @@ -29,11 +29,6 @@ #include "nsContentSecurityManager.h" #include "nsContentUtils.h" -#ifdef _WIN32_WINNT -#undef _WIN32_WINNT -#endif -#define _WIN32_WINNT 0x0600 - // we need windows.h to read out registry information... #include #include @@ -416,41 +411,27 @@ nsIconChannel::GetStockHIcon(nsIMozIconURI* aIconURI, { nsresult rv = NS_OK; - // We can only do this on Vista or above - HMODULE hShellDLL = ::LoadLibraryW(L"shell32.dll"); - decltype(SHGetStockIconInfo)* pSHGetStockIconInfo = - (decltype(SHGetStockIconInfo)*) ::GetProcAddress(hShellDLL, - "SHGetStockIconInfo"); - - if (pSHGetStockIconInfo) { - uint32_t desiredImageSize; - aIconURI->GetImageSize(&desiredImageSize); - nsAutoCString stockIcon; - aIconURI->GetStockIcon(stockIcon); - - SHSTOCKICONID stockIconID = GetStockIconIDForName(stockIcon); - if (stockIconID == SIID_INVALID) { - return NS_ERROR_NOT_AVAILABLE; - } + uint32_t desiredImageSize; + aIconURI->GetImageSize(&desiredImageSize); + nsAutoCString stockIcon; + aIconURI->GetStockIcon(stockIcon); - UINT infoFlags = SHGSI_ICON; - infoFlags |= GetSizeInfoFlag(desiredImageSize); + SHSTOCKICONID stockIconID = GetStockIconIDForName(stockIcon); + if (stockIconID == SIID_INVALID) { + return NS_ERROR_NOT_AVAILABLE; + } - SHSTOCKICONINFO sii = {0}; - sii.cbSize = sizeof(sii); - HRESULT hr = pSHGetStockIconInfo(stockIconID, infoFlags, &sii); + UINT infoFlags = SHGSI_ICON; + infoFlags |= GetSizeInfoFlag(desiredImageSize); - if (SUCCEEDED(hr)) { - *hIcon = sii.hIcon; - } else { - rv = NS_ERROR_FAILURE; - } - } else { - rv = NS_ERROR_NOT_AVAILABLE; - } + SHSTOCKICONINFO sii = {0}; + sii.cbSize = sizeof(sii); + HRESULT hr = SHGetStockIconInfo(stockIconID, infoFlags, &sii); - if (hShellDLL) { - ::FreeLibrary(hShellDLL); + if (SUCCEEDED(hr)) { + *hIcon = sii.hIcon; + } else { + rv = NS_ERROR_FAILURE; } return rv; diff --git a/toolkit/components/alerts/nsAlertsService.cpp b/toolkit/components/alerts/nsAlertsService.cpp index 35418dd176..dd67ad9831 100644 --- a/toolkit/components/alerts/nsAlertsService.cpp +++ b/toolkit/components/alerts/nsAlertsService.cpp @@ -22,6 +22,10 @@ #include "nsIFaviconService.h" #endif // MOZ_PLACES +#ifdef XP_WIN +#include +#endif + using namespace mozilla; using mozilla::dom::ContentChild; @@ -154,23 +158,12 @@ bool nsAlertsService::ShouldShowAlert() bool result = true; #ifdef XP_WIN - HMODULE shellDLL = ::LoadLibraryW(L"shell32.dll"); - if (!shellDLL) - return result; - - SHQueryUserNotificationStatePtr pSHQueryUserNotificationState = - (SHQueryUserNotificationStatePtr) ::GetProcAddress(shellDLL, "SHQueryUserNotificationState"); - - if (pSHQueryUserNotificationState) { - MOZ_QUERY_USER_NOTIFICATION_STATE qstate; - if (SUCCEEDED(pSHQueryUserNotificationState(&qstate))) { - if (qstate != QUNS_ACCEPTS_NOTIFICATIONS) { - result = false; - } + QUERY_USER_NOTIFICATION_STATE qstate; + if (SUCCEEDED(SHQueryUserNotificationState(&qstate))) { + if (qstate != QUNS_ACCEPTS_NOTIFICATIONS) { + result = false; } } - - ::FreeLibrary(shellDLL); #endif return result; diff --git a/toolkit/components/alerts/nsAlertsService.h b/toolkit/components/alerts/nsAlertsService.h index 3f23eaabfb..d2b2e1e6c0 100644 --- a/toolkit/components/alerts/nsAlertsService.h +++ b/toolkit/components/alerts/nsAlertsService.h @@ -10,23 +10,6 @@ #include "nsCOMPtr.h" #include "nsXULAlerts.h" -#ifdef XP_WIN -typedef enum tagMOZ_QUERY_USER_NOTIFICATION_STATE { - QUNS_NOT_PRESENT = 1, - QUNS_BUSY = 2, - QUNS_RUNNING_D3D_FULL_SCREEN = 3, - QUNS_PRESENTATION_MODE = 4, - QUNS_ACCEPTS_NOTIFICATIONS = 5, - QUNS_QUIET_TIME = 6, - QUNS_IMMERSIVE = 7 -} MOZ_QUERY_USER_NOTIFICATION_STATE; - -extern "C" { -// This function is Windows Vista or later -typedef HRESULT (__stdcall *SHQueryUserNotificationStatePtr)(MOZ_QUERY_USER_NOTIFICATION_STATE *pquns); -} -#endif // defined(XP_WIN) - class nsAlertsService : public nsIAlertsService, public nsIAlertsDoNotDisturb { diff --git a/toolkit/xre/nsEmbedFunctions.cpp b/toolkit/xre/nsEmbedFunctions.cpp index 0e85532b9d..1e67ea7ce4 100644 --- a/toolkit/xre/nsEmbedFunctions.cpp +++ b/toolkit/xre/nsEmbedFunctions.cpp @@ -24,6 +24,7 @@ #ifdef XP_WIN #include +#include #include "mozilla/ipc/WindowsMessageLoop.h" #endif @@ -119,10 +120,6 @@ using mozilla::startup::sChildProcessType; static NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID); -#ifdef XP_WIN -static const wchar_t kShellLibraryName[] = L"shell32.dll"; -#endif - nsresult XRE_LockProfileDirectory(nsIFile* aDirectory, nsISupports* *aLockObject) @@ -270,26 +267,9 @@ XRE_SetRemoteExceptionHandler(const char* aPipe/*= 0*/) void SetTaskbarGroupId(const nsString& aId) { - typedef HRESULT (WINAPI * SetCurrentProcessExplicitAppUserModelIDPtr)(PCWSTR AppID); - - SetCurrentProcessExplicitAppUserModelIDPtr funcAppUserModelID = nullptr; - - HMODULE hDLL = ::LoadLibraryW(kShellLibraryName); - - funcAppUserModelID = (SetCurrentProcessExplicitAppUserModelIDPtr) - GetProcAddress(hDLL, "SetCurrentProcessExplicitAppUserModelID"); - - if (!funcAppUserModelID) { - ::FreeLibrary(hDLL); - return; - } - - if (FAILED(funcAppUserModelID(aId.get()))) { + if (FAILED(SetCurrentProcessExplicitAppUserModelID(aId.get()))) { NS_WARNING("SetCurrentProcessExplicitAppUserModelID failed for child process."); } - - if (hDLL) - ::FreeLibrary(hDLL); } #endif diff --git a/widget/windows/WinTaskbar.cpp b/widget/windows/WinTaskbar.cpp index a2c553dea7..530cfd5b94 100644 --- a/widget/windows/WinTaskbar.cpp +++ b/widget/windows/WinTaskbar.cpp @@ -34,8 +34,6 @@ #include #include -const wchar_t kShellLibraryName[] = L"shell32.dll"; - static NS_DEFINE_CID(kJumpListBuilderCID, NS_WIN_JUMPLISTBUILDER_CID); namespace { @@ -77,30 +75,14 @@ SetWindowAppUserModelProp(mozIDOMWindow *aParent, if (!toplevelHWND) return NS_ERROR_INVALID_ARG; - typedef HRESULT (WINAPI * SHGetPropertyStoreForWindowPtr) - (HWND hwnd, REFIID riid, void** ppv); - SHGetPropertyStoreForWindowPtr funcGetProStore = nullptr; - - HMODULE hDLL = ::LoadLibraryW(kShellLibraryName); - funcGetProStore = (SHGetPropertyStoreForWindowPtr) - GetProcAddress(hDLL, "SHGetPropertyStoreForWindow"); - - if (!funcGetProStore) { - FreeLibrary(hDLL); - return NS_ERROR_NO_INTERFACE; - } - - IPropertyStore* pPropStore; - if (FAILED(funcGetProStore(toplevelHWND, - IID_PPV_ARGS(&pPropStore)))) { - FreeLibrary(hDLL); + RefPtr pPropStore; + if (FAILED(SHGetPropertyStoreForWindow(toplevelHWND, IID_IPropertyStore, + getter_AddRefs(pPropStore)))) { return NS_ERROR_INVALID_ARG; } PROPVARIANT pv; if (FAILED(InitPropVariantFromString(aIdentifier.get(), &pv))) { - pPropStore->Release(); - FreeLibrary(hDLL); return NS_ERROR_UNEXPECTED; } @@ -111,8 +93,6 @@ SetWindowAppUserModelProp(mozIDOMWindow *aParent, } PropVariantClear(&pv); - pPropStore->Release(); - FreeLibrary(hDLL); return rv; } @@ -339,30 +319,11 @@ WinTaskbar::GetDefaultGroupId(nsAString & aDefaultGroupId) { // (static) Called from AppShell bool WinTaskbar::RegisterAppUserModelID() { - SetCurrentProcessExplicitAppUserModelIDPtr funcAppUserModelID = nullptr; - bool retVal = false; - nsAutoString uid; if (!GetAppUserModelID(uid)) return false; - HMODULE hDLL = ::LoadLibraryW(kShellLibraryName); - - funcAppUserModelID = (SetCurrentProcessExplicitAppUserModelIDPtr) - GetProcAddress(hDLL, "SetCurrentProcessExplicitAppUserModelID"); - - if (!funcAppUserModelID) { - ::FreeLibrary(hDLL); - return false; - } - - if (SUCCEEDED(funcAppUserModelID(uid.get()))) - retVal = true; - - if (hDLL) - ::FreeLibrary(hDLL); - - return retVal; + return SUCCEEDED(SetCurrentProcessExplicitAppUserModelID(uid.get())); } NS_IMETHODIMP -- cgit v1.2.3