Source code

Revision control

Other Tools

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/. */
6
7
#ifndef mozilla_dom_serviceworkershutdownblocker_h__
8
#define mozilla_dom_serviceworkershutdownblocker_h__
9
10
#include "nsCOMPtr.h"
11
#include "nsIAsyncShutdown.h"
12
#include "nsISupportsImpl.h"
13
14
#include "mozilla/MozPromise.h"
15
16
namespace mozilla {
17
namespace dom {
18
19
/**
20
* Main thread only.
21
*/
22
class ServiceWorkerShutdownBlocker final : public nsIAsyncShutdownBlocker {
23
public:
24
NS_DECL_ISUPPORTS
25
NS_DECL_NSIASYNCSHUTDOWNBLOCKER
26
27
/**
28
* Returns the registered shutdown blocker if registration succeeded and
29
* nullptr otherwise.
30
*/
31
static already_AddRefed<ServiceWorkerShutdownBlocker> CreateAndRegisterOn(
32
nsIAsyncShutdownClient* aShutdownBarrier);
33
34
/**
35
* Blocks shutdown until `aPromise` settles.
36
*
37
* Can be called multiple times, and shutdown will be blocked until all the
38
* calls' promises settle, but all of these calls must happen before
39
* `StopAcceptingPromises()` is called (assertions will enforce this).
40
*/
41
void WaitOnPromise(GenericNonExclusivePromise* aPromise);
42
43
/**
44
* Once this is called, shutdown will be blocked until all promises
45
* passed to `WaitOnPromise()` settle, and there must be no more calls to
46
* `WaitOnPromise()` (assertions will enforce this).
47
*/
48
void StopAcceptingPromises();
49
50
private:
51
ServiceWorkerShutdownBlocker();
52
53
~ServiceWorkerShutdownBlocker();
54
55
/**
56
* No-op if any of the following are true:
57
* 1) `BlockShutdown()` hasn't been called yet, or
58
* 2) `StopAcceptingPromises()` hasn't been called yet, or
59
* 3) `StopAcceptingPromises()` HAS been called, but there are still pending
60
* promises.
61
*/
62
void MaybeUnblockShutdown();
63
64
/**
65
* Returns the remaining pending promise count (i.e. excluding the promise
66
* that just settled).
67
*/
68
uint32_t PromiseSettled();
69
70
bool IsAcceptingPromises() const;
71
72
uint32_t GetPendingPromises() const;
73
74
struct AcceptingPromises {
75
uint32_t mPendingPromises = 0;
76
};
77
78
struct NotAcceptingPromises {
79
explicit NotAcceptingPromises(AcceptingPromises aPreviousState);
80
81
uint32_t mPendingPromises = 0;
82
};
83
84
Variant<AcceptingPromises, NotAcceptingPromises> mState;
85
86
nsCOMPtr<nsIAsyncShutdownClient> mShutdownClient;
87
};
88
89
} // namespace dom
90
} // namespace mozilla
91
92
#endif // mozilla_dom_serviceworkershutdownblocker_h__