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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
/* 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 "Shutdown.h"
#include "mozilla/Unused.h"
namespace mozilla {
namespace places {
uint16_t PlacesShutdownBlocker::sCounter = 0;
Atomic<bool> PlacesShutdownBlocker::sIsStarted(false);
PlacesShutdownBlocker::PlacesShutdownBlocker(const nsString& aName)
: mName(aName)
, mState(NOT_STARTED)
, mCounter(sCounter++)
{
MOZ_ASSERT(NS_IsMainThread());
// During tests, we can end up with the Database singleton being resurrected.
// Make sure that each instance of DatabaseShutdown has a unique name.
if (mCounter > 1) {
mName.AppendInt(mCounter);
}
}
// nsIAsyncShutdownBlocker
NS_IMETHODIMP
PlacesShutdownBlocker::GetName(nsAString& aName)
{
aName = mName;
return NS_OK;
}
// nsIAsyncShutdownBlocker
NS_IMETHODIMP
PlacesShutdownBlocker::GetState(nsIPropertyBag** _state)
{
NS_ENSURE_ARG_POINTER(_state);
nsCOMPtr<nsIWritablePropertyBag2> bag =
do_CreateInstance("@mozilla.org/hash-property-bag;1");
NS_ENSURE_TRUE(bag, NS_ERROR_OUT_OF_MEMORY);
bag.forget(_state);
// Put `mState` in field `progress`
RefPtr<nsVariant> progress = new nsVariant();
nsresult rv = progress->SetAsUint8(mState);
if (NS_WARN_IF(NS_FAILED(rv))) return rv;
rv = static_cast<nsIWritablePropertyBag2*>(*_state)->SetPropertyAsInterface(
NS_LITERAL_STRING("progress"), progress);
if (NS_WARN_IF(NS_FAILED(rv))) return rv;
// Put `mBarrier`'s state in field `barrier`, if possible
if (!mBarrier) {
return NS_OK;
}
nsCOMPtr<nsIPropertyBag> barrierState;
rv = mBarrier->GetState(getter_AddRefs(barrierState));
if (NS_FAILED(rv)) {
return NS_OK;
}
RefPtr<nsVariant> barrier = new nsVariant();
rv = barrier->SetAsInterface(NS_GET_IID(nsIPropertyBag), barrierState);
if (NS_WARN_IF(NS_FAILED(rv))) return rv;
rv = static_cast<nsIWritablePropertyBag2*>(*_state)->SetPropertyAsInterface(
NS_LITERAL_STRING("Barrier"), barrier);
if (NS_WARN_IF(NS_FAILED(rv))) return rv;
return NS_OK;
}
// nsIAsyncShutdownBlocker
NS_IMETHODIMP
PlacesShutdownBlocker::BlockShutdown(nsIAsyncShutdownClient* aParentClient)
{
MOZ_ASSERT(false, "should always be overridden");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMPL_ISUPPORTS(
PlacesShutdownBlocker,
nsIAsyncShutdownBlocker
)
////////////////////////////////////////////////////////////////////////////////
ClientsShutdownBlocker::ClientsShutdownBlocker()
: PlacesShutdownBlocker(NS_LITERAL_STRING("Places Clients shutdown"))
{
MOZ_ASSERT(NS_IsMainThread());
// Create a barrier that will be exposed to clients through GetClient(), so
// they can block Places shutdown.
nsCOMPtr<nsIAsyncShutdownService> asyncShutdown = services::GetAsyncShutdown();
MOZ_ASSERT(asyncShutdown);
if (asyncShutdown) {
nsCOMPtr<nsIAsyncShutdownBarrier> barrier;
MOZ_ALWAYS_SUCCEEDS(asyncShutdown->MakeBarrier(mName, getter_AddRefs(barrier)));
mBarrier = new nsMainThreadPtrHolder<nsIAsyncShutdownBarrier>(barrier);
}
}
already_AddRefed<nsIAsyncShutdownClient>
ClientsShutdownBlocker::GetClient()
{
nsCOMPtr<nsIAsyncShutdownClient> client;
if (mBarrier) {
MOZ_ALWAYS_SUCCEEDS(mBarrier->GetClient(getter_AddRefs(client)));
}
return client.forget();
}
// nsIAsyncShutdownBlocker
NS_IMETHODIMP
ClientsShutdownBlocker::BlockShutdown(nsIAsyncShutdownClient* aParentClient)
{
MOZ_ASSERT(NS_IsMainThread());
mParentClient = new nsMainThreadPtrHolder<nsIAsyncShutdownClient>(aParentClient);
mState = RECEIVED_BLOCK_SHUTDOWN;
if (NS_WARN_IF(!mBarrier)) {
return NS_ERROR_NOT_AVAILABLE;
}
// Wait until all the clients have removed their blockers.
MOZ_ALWAYS_SUCCEEDS(mBarrier->Wait(this));
mState = CALLED_WAIT_CLIENTS;
return NS_OK;
}
// nsIAsyncShutdownCompletionCallback
NS_IMETHODIMP
ClientsShutdownBlocker::Done()
{
// At this point all the clients are done, we can stop blocking the shutdown
// phase.
mState = RECEIVED_DONE;
// mParentClient is nullptr in tests.
if (mParentClient) {
nsresult rv = mParentClient->RemoveBlocker(this);
if (NS_WARN_IF(NS_FAILED(rv))) return rv;
mParentClient = nullptr;
}
mBarrier = nullptr;
return NS_OK;
}
NS_IMPL_ISUPPORTS_INHERITED(
ClientsShutdownBlocker,
PlacesShutdownBlocker,
nsIAsyncShutdownCompletionCallback
)
////////////////////////////////////////////////////////////////////////////////
ConnectionShutdownBlocker::ConnectionShutdownBlocker(Database* aDatabase)
: PlacesShutdownBlocker(NS_LITERAL_STRING("Places Connection shutdown"))
, mDatabase(aDatabase)
{
// Do nothing.
}
// nsIAsyncShutdownBlocker
NS_IMETHODIMP
ConnectionShutdownBlocker::BlockShutdown(nsIAsyncShutdownClient* aParentClient)
{
MOZ_ASSERT(NS_IsMainThread());
mParentClient = new nsMainThreadPtrHolder<nsIAsyncShutdownClient>(aParentClient);
mState = RECEIVED_BLOCK_SHUTDOWN;
// Annotate that Database shutdown started.
sIsStarted = true;
// Fire internal database closing notification.
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
MOZ_ASSERT(os);
if (os) {
Unused << os->NotifyObservers(nullptr, TOPIC_PLACES_WILL_CLOSE_CONNECTION, nullptr);
}
mState = NOTIFIED_OBSERVERS_PLACES_WILL_CLOSE_CONNECTION;
// At this stage, any use of this database is forbidden. Get rid of
// `gDatabase`. Note, however, that the database could be
// resurrected. This can happen in particular during tests.
MOZ_ASSERT(Database::gDatabase == nullptr || Database::gDatabase == mDatabase);
Database::gDatabase = nullptr;
// Database::Shutdown will invoke Complete once the connection is closed.
mDatabase->Shutdown();
mState = CALLED_STORAGESHUTDOWN;
return NS_OK;
}
// mozIStorageCompletionCallback
NS_IMETHODIMP
ConnectionShutdownBlocker::Complete(nsresult, nsISupports*)
{
MOZ_ASSERT(NS_IsMainThread());
mState = RECEIVED_STORAGESHUTDOWN_COMPLETE;
// The connection is closed, the Database has no more use, so we can break
// possible cycles.
mDatabase = nullptr;
// Notify the connection has gone.
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
MOZ_ASSERT(os);
if (os) {
MOZ_ALWAYS_SUCCEEDS(os->NotifyObservers(nullptr,
TOPIC_PLACES_CONNECTION_CLOSED,
nullptr));
}
mState = NOTIFIED_OBSERVERS_PLACES_CONNECTION_CLOSED;
// mParentClient is nullptr in tests
if (mParentClient) {
nsresult rv = mParentClient->RemoveBlocker(this);
if (NS_WARN_IF(NS_FAILED(rv))) return rv;
mParentClient = nullptr;
}
return NS_OK;
}
NS_IMPL_ISUPPORTS_INHERITED(
ConnectionShutdownBlocker,
PlacesShutdownBlocker,
mozIStorageCompletionCallback
)
} // namespace places
} // namespace mozilla
|