diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /netwerk/cache2/CachePurgeLock.cpp | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'netwerk/cache2/CachePurgeLock.cpp')
-rw-r--r-- | netwerk/cache2/CachePurgeLock.cpp | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/netwerk/cache2/CachePurgeLock.cpp b/netwerk/cache2/CachePurgeLock.cpp new file mode 100644 index 0000000000..6da0ddc0dd --- /dev/null +++ b/netwerk/cache2/CachePurgeLock.cpp @@ -0,0 +1,76 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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/. */ + +#include "CachePurgeLock.h" +#include "nsCOMPtr.h" +#include "nsIFile.h" +#include "nsAppRunner.h" +#include "mozilla/MultiInstanceLock.h" + +namespace mozilla::net { + +NS_IMPL_ISUPPORTS(CachePurgeLock, nsICachePurgeLock) + +NS_IMETHODIMP +CachePurgeLock::Lock(const nsACString& profileName) { + nsresult rv; + if (mLock != MULTI_INSTANCE_LOCK_HANDLE_ERROR) { + // Lock is already open. + return NS_OK; + } + + nsCOMPtr<nsIFile> appFile = mozilla::GetNormalizedAppFile(nullptr); + if (!appFile) { + return NS_ERROR_NOT_AVAILABLE; + } + + nsCOMPtr<nsIFile> appDirFile; + rv = appFile->GetParent(getter_AddRefs(appDirFile)); + NS_ENSURE_SUCCESS(rv, rv); + + nsAutoString appDirPath; + rv = appDirFile->GetPath(appDirPath); + NS_ENSURE_SUCCESS(rv, rv); + + nsAutoCString lockName(profileName); + lockName.Append("-cachePurge"); + + mLock = mozilla::OpenMultiInstanceLock(lockName.get(), appDirPath.get()); + if (mLock == MULTI_INSTANCE_LOCK_HANDLE_ERROR) { + return NS_ERROR_FAILURE; + } + + return NS_OK; +} + +NS_IMETHODIMP +CachePurgeLock::IsOtherInstanceRunning(bool* aResult) { + if (NS_WARN_IF(XRE_GetProcessType() != GeckoProcessType_Default)) { + return NS_ERROR_SERVICE_NOT_AVAILABLE; + } + + if (mLock == MULTI_INSTANCE_LOCK_HANDLE_ERROR) { + return NS_ERROR_NOT_INITIALIZED; + } + + bool rv = mozilla::IsOtherInstanceRunning(mLock, aResult); + NS_ENSURE_TRUE(rv, NS_ERROR_FAILURE); + + return NS_OK; +} + +NS_IMETHODIMP +CachePurgeLock::Unlock() { + if (mLock == MULTI_INSTANCE_LOCK_HANDLE_ERROR) { + // Lock is already released. + return NS_OK; + } + + mozilla::ReleaseMultiInstanceLock(mLock); + mLock = MULTI_INSTANCE_LOCK_HANDLE_ERROR; + + return NS_OK; +} + +} // namespace mozilla::net |