diff --git a/browser/components/shell/moz.build b/browser/components/shell/moz.build index 28cf72a31e80..d4a234b4a069 100644 --- a/browser/components/shell/moz.build +++ b/browser/components/shell/moz.build @@ -4,6 +4,11 @@ # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. +# For BinaryPath::GetLong for Windows +LOCAL_INCLUDES += [ + '/xpcom/build' +] + XPCSHELL_TESTS_MANIFESTS += ['test/unit/xpcshell.ini'] BROWSER_CHROME_MANIFESTS += ['test/browser.ini'] diff --git a/browser/components/shell/nsWindowsShellService.cpp b/browser/components/shell/nsWindowsShellService.cpp index 288ca84aa484..52631992721f 100644 --- a/browser/components/shell/nsWindowsShellService.cpp +++ b/browser/components/shell/nsWindowsShellService.cpp @@ -5,6 +5,7 @@ #include "nsWindowsShellService.h" +#include "BinaryPath.h" #include "city.h" #include "imgIContainer.h" #include "imgIRequest.h" @@ -228,13 +229,10 @@ nsWindowsShellService::IsDefaultBrowser(bool aStartupCheck, return NS_OK; } - wchar_t exePath[MAX_BUF] = L""; - if (!::GetModuleFileNameW(0, exePath, MAX_BUF)) { - return NS_OK; - } - // Convert the path to a long path since GetModuleFileNameW returns the path - // that was used to launch Firefox which is not necessarily a long path. - if (!::GetLongPathNameW(exePath, exePath, MAX_BUF)) { + wchar_t exePath[MAXPATHLEN] = L""; + nsresult rv = BinaryPath::GetLong(exePath); + + if (NS_FAILED(rv)) { return NS_OK; } diff --git a/xpcom/build/BinaryPath.h b/xpcom/build/BinaryPath.h index 0160d7e0d0e5..8ec672f715c2 100644 --- a/xpcom/build/BinaryPath.h +++ b/xpcom/build/BinaryPath.h @@ -20,6 +20,8 @@ #include "mozilla/UniquePtrExtensions.h" #ifdef MOZILLA_INTERNAL_API +#include "nsCOMPtr.h" +#include "nsIFile.h" #include "nsString.h" #endif @@ -41,13 +43,51 @@ public: return NS_OK; } + static nsresult GetLong(wchar_t aResult[MAXPATHLEN]) + { + static bool cached = false; + static wchar_t exeLongPath[MAXPATHLEN] = L""; + + if (!cached) { + nsresult rv = GetW(nullptr, exeLongPath); + + if (NS_FAILED(rv)) { + return rv; + } + + if (!::GetLongPathNameW(exeLongPath, exeLongPath, MAXPATHLEN)) { + return NS_ERROR_FAILURE; + } + + cached = true; + } + + if (wcscpy_s(aResult, MAXPATHLEN, exeLongPath)) { + return NS_ERROR_FAILURE; + } + + return NS_OK; + } + private: static nsresult GetW(const char* argv0, wchar_t aResult[MAXPATHLEN]) { - if (::GetModuleFileNameW(0, aResult, MAXPATHLEN)) { - return NS_OK; + static bool cached = false; + static wchar_t moduleFileName[MAXPATHLEN] = L""; + + if (!cached) { + if (!::GetModuleFileNameW(0, moduleFileName, MAXPATHLEN)) { + return NS_ERROR_FAILURE; + } + + cached = true; } - return NS_ERROR_FAILURE; + + if (wcscpy_s(aResult, MAXPATHLEN, moduleFileName)) { + return NS_ERROR_FAILURE; + } + + return NS_OK; } #elif defined(XP_MACOSX)