summaryrefslogtreecommitdiffstats
path: root/netwerk/cache2/CachePurgeLock.cpp
blob: 6da0ddc0ddeb5be62868a02d0a2ec990c15924ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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