Backed out 8 changesets (bug 1873776) for causing vendor failures. CLOSED TREE
[gecko.git] / dom / fs / child / FileSystemShutdownBlocker.cpp
blobbddbf18c6b25f4742c54a279c3337be9698e1f0a
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "fs/FileSystemShutdownBlocker.h"
9 #include "MainThreadUtils.h"
10 #include "mozilla/Services.h"
11 #include "mozilla/dom/quota/QuotaCommon.h"
12 #include "mozilla/dom/quota/ResultExtensions.h"
13 #include "nsComponentManagerUtils.h"
14 #include "nsIAsyncShutdown.h"
15 #include "nsISupportsImpl.h"
16 #include "nsIWritablePropertyBag2.h"
17 #include "nsStringFwd.h"
19 namespace mozilla::dom::fs {
21 namespace {
23 nsString CreateBlockerName() {
24 const int32_t blockerIdLength = 32;
25 nsAutoCString blockerId;
26 blockerId.SetLength(blockerIdLength);
27 NS_MakeRandomString(blockerId.BeginWriting(), blockerIdLength);
29 nsString blockerName = u"OPFS_"_ns;
30 blockerName.Append(NS_ConvertUTF8toUTF16(blockerId));
32 return blockerName;
35 class FileSystemWritableBlocker : public FileSystemShutdownBlocker {
36 public:
37 FileSystemWritableBlocker() : mName(CreateBlockerName()) {}
39 NS_DECL_ISUPPORTS_INHERITED
40 NS_DECL_NSIASYNCSHUTDOWNBLOCKER
42 NS_IMETHODIMP Block() override;
44 NS_IMETHODIMP Unblock() override;
46 protected:
47 virtual ~FileSystemWritableBlocker() = default;
49 Result<already_AddRefed<nsIAsyncShutdownClient>, nsresult> GetBarrier() const;
51 private:
52 const nsString mName;
55 NS_IMPL_ISUPPORTS_INHERITED(FileSystemWritableBlocker,
56 FileSystemShutdownBlocker, nsIAsyncShutdownBlocker)
58 NS_IMETHODIMP FileSystemWritableBlocker::Block() {
59 MOZ_ASSERT(NS_IsMainThread());
60 QM_TRY_UNWRAP(nsCOMPtr<nsIAsyncShutdownClient> barrier, GetBarrier());
62 QM_TRY(MOZ_TO_RESULT(barrier->AddBlocker(
63 this, NS_ConvertUTF8toUTF16(nsCString(__FILE__)), __LINE__,
64 NS_ConvertUTF8toUTF16(nsCString(__func__)))));
66 return NS_OK;
69 NS_IMETHODIMP FileSystemWritableBlocker::Unblock() {
70 MOZ_ASSERT(NS_IsMainThread());
71 QM_TRY_UNWRAP(nsCOMPtr<nsIAsyncShutdownClient> barrier, GetBarrier());
73 MOZ_ASSERT(NS_SUCCEEDED(barrier->RemoveBlocker(this)));
75 return NS_OK;
78 Result<already_AddRefed<nsIAsyncShutdownClient>, nsresult>
79 FileSystemWritableBlocker::GetBarrier() const {
80 nsCOMPtr<nsIAsyncShutdownService> svc = services::GetAsyncShutdownService();
81 QM_TRY(OkIf(svc), Err(NS_ERROR_FAILURE));
83 nsCOMPtr<nsIAsyncShutdownClient> barrier;
84 QM_TRY(MOZ_TO_RESULT(svc->GetXpcomWillShutdown(getter_AddRefs(barrier))));
86 return barrier.forget();
89 NS_IMETHODIMP
90 FileSystemWritableBlocker::GetName(nsAString& aName) {
91 aName = mName;
93 return NS_OK;
96 NS_IMETHODIMP
97 FileSystemWritableBlocker::GetState(nsIPropertyBag** aBagOut) {
98 MOZ_ASSERT(aBagOut);
100 nsCOMPtr<nsIWritablePropertyBag2> propertyBag =
101 do_CreateInstance("@mozilla.org/hash-property-bag;1");
103 QM_TRY(OkIf(propertyBag), NS_ERROR_OUT_OF_MEMORY)
105 propertyBag.forget(aBagOut);
107 return NS_OK;
110 NS_IMETHODIMP
111 FileSystemWritableBlocker::BlockShutdown(
112 nsIAsyncShutdownClient* /* aBarrier */) {
113 MOZ_ASSERT(NS_IsMainThread());
115 return NS_OK;
118 class FileSystemNullBlocker : public FileSystemShutdownBlocker {
119 public:
120 NS_IMETHODIMP Block() override { return NS_OK; }
122 NS_IMETHODIMP Unblock() override { return NS_OK; }
124 protected:
125 virtual ~FileSystemNullBlocker() = default;
128 } // namespace
130 /* static */
131 already_AddRefed<FileSystemShutdownBlocker>
132 FileSystemShutdownBlocker::CreateForWritable() {
133 // The shutdown blocker watches for xpcom-will-shutdown which is not fired
134 // during content process shutdown in release builds.
135 #ifdef DEBUG
136 if (NS_IsMainThread()) {
137 RefPtr<FileSystemShutdownBlocker> shutdownBlocker =
138 new FileSystemWritableBlocker();
140 return shutdownBlocker.forget();
142 #endif
144 RefPtr<FileSystemShutdownBlocker> shutdownBlocker =
145 new FileSystemNullBlocker();
147 return shutdownBlocker.forget();
150 NS_IMPL_ISUPPORTS(FileSystemShutdownBlocker, nsIAsyncShutdownBlocker)
152 /* nsIAsyncShutdownBlocker methods */
153 NS_IMETHODIMP
154 FileSystemShutdownBlocker::GetName(nsAString& /* aName */) { return NS_OK; }
156 NS_IMETHODIMP
157 FileSystemShutdownBlocker::GetState(nsIPropertyBag** /* aBagOut */) {
158 return NS_OK;
161 NS_IMETHODIMP
162 FileSystemShutdownBlocker::BlockShutdown(
163 nsIAsyncShutdownClient* /* aBarrier */) {
164 return NS_OK;
167 } // namespace mozilla::dom::fs