Bug 1705676 - Move GetNormalizedAppFile function to MultiInstanceLock header r=nalexander

Depends on D126339

Differential Revision: https://phabricator.services.mozilla.com/D132595
This commit is contained in:
Valentin Gosu 2022-08-15 07:07:45 +00:00
Родитель 00ed953e23
Коммит 5f6fa390ae
3 изменённых файлов: 69 добавлений и 48 удалений

Просмотреть файл

@ -20,6 +20,14 @@
# include <sys/types.h>
#endif
#ifdef XP_WIN
# include "WinUtils.h"
#endif
#ifdef MOZ_WIDGET_COCOA
# include "nsILocalFileMac.h"
#endif
namespace mozilla {
static bool GetLockFileName(const char* nameToken, const char16_t* installPath,
@ -237,4 +245,51 @@ bool IsOtherInstanceRunning(MultiInstLockHandle lock, bool* aResult) {
#endif
}
already_AddRefed<nsIFile> GetNormalizedAppFile(nsIFile* aAppFile) {
// If we're given an app file, use it; otherwise, get it from the ambient
// directory service.
nsresult rv;
nsCOMPtr<nsIFile> appFile;
if (aAppFile) {
rv = aAppFile->Clone(getter_AddRefs(appFile));
NS_ENSURE_SUCCESS(rv, nullptr);
} else {
nsCOMPtr<nsIProperties> dirSvc =
do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID);
NS_ENSURE_TRUE(dirSvc, nullptr);
rv = dirSvc->Get(XRE_EXECUTABLE_FILE, NS_GET_IID(nsIFile),
getter_AddRefs(appFile));
NS_ENSURE_SUCCESS(rv, nullptr);
}
// It is possible that the path we have is on a case insensitive
// filesystem in which case the path may vary depending on how the
// application is called. We want to normalize the case somehow.
// On Linux XRE_EXECUTABLE_FILE already seems to be set to the correct path.
//
// See similar nsXREDirProvider::GetInstallHash. The main difference here is
// to allow lookup to fail on OSX, because some tests use a nonexistent
// appFile.
#ifdef XP_WIN
// Windows provides a way to get the correct case.
if (!mozilla::widget::WinUtils::ResolveJunctionPointsAndSymLinks(appFile)) {
NS_WARNING("Failed to resolve install directory.");
}
#elif defined(MOZ_WIDGET_COCOA)
// On OSX roundtripping through an FSRef fixes the case.
FSRef ref;
nsCOMPtr<nsILocalFileMac> macFile = do_QueryInterface(appFile);
if (macFile && NS_SUCCEEDED(macFile->GetFSRef(&ref)) &&
NS_SUCCEEDED(
NS_NewLocalFileWithFSRef(&ref, true, getter_AddRefs(macFile)))) {
appFile = static_cast<nsIFile*>(macFile);
} else {
NS_WARNING("Failed to resolve install directory.");
}
#endif
return appFile.forget();
}
}; // namespace mozilla

Просмотреть файл

@ -79,6 +79,14 @@ void ReleaseMultiInstanceLock(MultiInstLockHandle lock);
// Return value is true on success, false on error (and aResult won't be set).
bool IsOtherInstanceRunning(MultiInstLockHandle lock, bool* aResult);
// It is possible that the path we have is on a case insensitive
// filesystem in which case the path may vary depending on how the
// application is called. We want to normalize the case somehow.
// When aAppFile is NULL, this function returns a nsIFile with a normalized
// path for the currently running binary. When aAppFile is not null,
// this function ensures the file path is properly normalized.
already_AddRefed<nsIFile> GetNormalizedAppFile(nsIFile* aAppFile);
}; // namespace mozilla
#endif // MULTIINSTANCELOCK_H

Просмотреть файл

@ -16,10 +16,6 @@
#include "nsString.h"
#include "nsXULAppAPI.h"
#ifdef XP_WIN
# include "WinUtils.h"
#endif
// The lock code generates a path that already includes the vendor name,
// so this only needs to name the specific lock.
#define UPDATE_LOCK_NAME_TOKEN "UpdateLock"
@ -71,60 +67,22 @@ NS_IMETHODIMP nsUpdateSyncManager::Observe(nsISupports* aSubject,
}
nsresult nsUpdateSyncManager::OpenLock(nsIFile* anAppFile) {
nsresult rv;
if (mLock != MULTI_INSTANCE_LOCK_HANDLE_ERROR) {
// Lock is already open.
return NS_OK;
}
// Only open the lock from the browser process.
// Our component registration should already have made sure of this.
if (NS_WARN_IF(XRE_GetProcessType() != GeckoProcessType_Default)) {
return NS_OK;
}
// If we're given an app file, use it; otherwise, get it from the ambient
// directory service.
nsresult rv;
nsCOMPtr<nsIFile> appFile;
if (anAppFile) {
rv = anAppFile->Clone(getter_AddRefs(appFile));
NS_ENSURE_SUCCESS(rv, rv);
} else {
nsCOMPtr<nsIProperties> dirSvc =
do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID);
NS_ENSURE_TRUE(dirSvc, NS_ERROR_SERVICE_NOT_AVAILABLE);
rv = dirSvc->Get(XRE_EXECUTABLE_FILE, NS_GET_IID(nsIFile),
getter_AddRefs(appFile));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIFile> appFile = mozilla::GetNormalizedAppFile(anAppFile);
if (!appFile) {
return NS_ERROR_NOT_AVAILABLE;
}
// It is possible that the path we have is on a case insensitive
// filesystem in which case the path may vary depending on how the
// application is called. We want to normalize the case somehow.
// On Linux XRE_EXECUTABLE_FILE already seems to be set to the correct path.
//
// See similar nsXREDirProvider::GetInstallHash. The main difference here is
// to allow lookup to fail on OSX, because some tests use a nonexistent
// appFile.
#ifdef XP_WIN
// Windows provides a way to get the correct case.
if (!mozilla::widget::WinUtils::ResolveJunctionPointsAndSymLinks(appFile)) {
NS_WARNING("Failed to resolve install directory.");
}
#elif defined(MOZ_WIDGET_COCOA)
// On OSX roundtripping through an FSRef fixes the case.
FSRef ref;
nsCOMPtr<nsILocalFileMac> macFile = do_QueryInterface(appFile);
if (macFile && NS_SUCCEEDED(macFile->GetFSRef(&ref)) &&
NS_SUCCEEDED(
NS_NewLocalFileWithFSRef(&ref, true, getter_AddRefs(macFile)))) {
appFile = static_cast<nsIFile*>(macFile);
} else {
NS_WARNING("Failed to resolve install directory.");
}
#endif
nsCOMPtr<nsIFile> appDirFile;
rv = appFile->GetParent(getter_AddRefs(appDirFile));
NS_ENSURE_SUCCESS(rv, rv);
@ -133,8 +91,8 @@ nsresult nsUpdateSyncManager::OpenLock(nsIFile* anAppFile) {
rv = appDirFile->GetPath(appDirPath);
NS_ENSURE_SUCCESS(rv, rv);
mLock = mozilla::OpenMultiInstanceLock(UPDATE_LOCK_NAME_TOKEN,
PromiseFlatString(appDirPath).get());
mLock =
mozilla::OpenMultiInstanceLock(UPDATE_LOCK_NAME_TOKEN, appDirPath.get());
NS_ENSURE_TRUE(mLock, NS_ERROR_FAILURE);
return NS_OK;