summaryrefslogtreecommitdiffstats
path: root/dom/fs/child/FileSystemShutdownBlocker.cpp
blob: bddbf18c6b25f4742c54a279c3337be9698e1f0a (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "fs/FileSystemShutdownBlocker.h"

#include "MainThreadUtils.h"
#include "mozilla/Services.h"
#include "mozilla/dom/quota/QuotaCommon.h"
#include "mozilla/dom/quota/ResultExtensions.h"
#include "nsComponentManagerUtils.h"
#include "nsIAsyncShutdown.h"
#include "nsISupportsImpl.h"
#include "nsIWritablePropertyBag2.h"
#include "nsStringFwd.h"

namespace mozilla::dom::fs {

namespace {

nsString CreateBlockerName() {
  const int32_t blockerIdLength = 32;
  nsAutoCString blockerId;
  blockerId.SetLength(blockerIdLength);
  NS_MakeRandomString(blockerId.BeginWriting(), blockerIdLength);

  nsString blockerName = u"OPFS_"_ns;
  blockerName.Append(NS_ConvertUTF8toUTF16(blockerId));

  return blockerName;
}

class FileSystemWritableBlocker : public FileSystemShutdownBlocker {
 public:
  FileSystemWritableBlocker() : mName(CreateBlockerName()) {}

  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_NSIASYNCSHUTDOWNBLOCKER

  NS_IMETHODIMP Block() override;

  NS_IMETHODIMP Unblock() override;

 protected:
  virtual ~FileSystemWritableBlocker() = default;

  Result<already_AddRefed<nsIAsyncShutdownClient>, nsresult> GetBarrier() const;

 private:
  const nsString mName;
};

NS_IMPL_ISUPPORTS_INHERITED(FileSystemWritableBlocker,
                            FileSystemShutdownBlocker, nsIAsyncShutdownBlocker)

NS_IMETHODIMP FileSystemWritableBlocker::Block() {
  MOZ_ASSERT(NS_IsMainThread());
  QM_TRY_UNWRAP(nsCOMPtr<nsIAsyncShutdownClient> barrier, GetBarrier());

  QM_TRY(MOZ_TO_RESULT(barrier->AddBlocker(
      this, NS_ConvertUTF8toUTF16(nsCString(__FILE__)), __LINE__,
      NS_ConvertUTF8toUTF16(nsCString(__func__)))));

  return NS_OK;
}

NS_IMETHODIMP FileSystemWritableBlocker::Unblock() {
  MOZ_ASSERT(NS_IsMainThread());
  QM_TRY_UNWRAP(nsCOMPtr<nsIAsyncShutdownClient> barrier, GetBarrier());

  MOZ_ASSERT(NS_SUCCEEDED(barrier->RemoveBlocker(this)));

  return NS_OK;
}

Result<already_AddRefed<nsIAsyncShutdownClient>, nsresult>
FileSystemWritableBlocker::GetBarrier() const {
  nsCOMPtr<nsIAsyncShutdownService> svc = services::GetAsyncShutdownService();
  QM_TRY(OkIf(svc), Err(NS_ERROR_FAILURE));

  nsCOMPtr<nsIAsyncShutdownClient> barrier;
  QM_TRY(MOZ_TO_RESULT(svc->GetXpcomWillShutdown(getter_AddRefs(barrier))));

  return barrier.forget();
}

NS_IMETHODIMP
FileSystemWritableBlocker::GetName(nsAString& aName) {
  aName = mName;

  return NS_OK;
}

NS_IMETHODIMP
FileSystemWritableBlocker::GetState(nsIPropertyBag** aBagOut) {
  MOZ_ASSERT(aBagOut);

  nsCOMPtr<nsIWritablePropertyBag2> propertyBag =
      do_CreateInstance("@mozilla.org/hash-property-bag;1");

  QM_TRY(OkIf(propertyBag), NS_ERROR_OUT_OF_MEMORY)

  propertyBag.forget(aBagOut);

  return NS_OK;
}

NS_IMETHODIMP
FileSystemWritableBlocker::BlockShutdown(
    nsIAsyncShutdownClient* /* aBarrier */) {
  MOZ_ASSERT(NS_IsMainThread());

  return NS_OK;
}

class FileSystemNullBlocker : public FileSystemShutdownBlocker {
 public:
  NS_IMETHODIMP Block() override { return NS_OK; }

  NS_IMETHODIMP Unblock() override { return NS_OK; }

 protected:
  virtual ~FileSystemNullBlocker() = default;
};

}  // namespace

/* static */
already_AddRefed<FileSystemShutdownBlocker>
FileSystemShutdownBlocker::CreateForWritable() {
// The shutdown blocker watches for xpcom-will-shutdown which is not fired
// during content process shutdown in release builds.
#ifdef DEBUG
  if (NS_IsMainThread()) {
    RefPtr<FileSystemShutdownBlocker> shutdownBlocker =
        new FileSystemWritableBlocker();

    return shutdownBlocker.forget();
  }
#endif

  RefPtr<FileSystemShutdownBlocker> shutdownBlocker =
      new FileSystemNullBlocker();

  return shutdownBlocker.forget();
}

NS_IMPL_ISUPPORTS(FileSystemShutdownBlocker, nsIAsyncShutdownBlocker)

/* nsIAsyncShutdownBlocker methods */
NS_IMETHODIMP
FileSystemShutdownBlocker::GetName(nsAString& /* aName */) { return NS_OK; }

NS_IMETHODIMP
FileSystemShutdownBlocker::GetState(nsIPropertyBag** /* aBagOut */) {
  return NS_OK;
}

NS_IMETHODIMP
FileSystemShutdownBlocker::BlockShutdown(
    nsIAsyncShutdownClient* /* aBarrier */) {
  return NS_OK;
}

}  // namespace mozilla::dom::fs