Bug 1800109 - Add a test that checks that overflowed toolbar items in the addons...
[gecko.git] / storage / mozStorageService.cpp
blob7b5edae60e6f2cae8d12f0d231c8f884502f91e4
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
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 "mozilla/Attributes.h"
8 #include "mozilla/DebugOnly.h"
9 #include "mozilla/SpinEventLoopUntil.h"
11 #include "mozStorageService.h"
12 #include "mozStorageConnection.h"
13 #include "nsComponentManagerUtils.h"
14 #include "nsEmbedCID.h"
15 #include "nsExceptionHandler.h"
16 #include "nsThreadUtils.h"
17 #include "mozStoragePrivateHelpers.h"
18 #include "nsIObserverService.h"
19 #include "nsIPropertyBag2.h"
20 #include "mozilla/Services.h"
21 #include "mozilla/LateWriteChecks.h"
22 #include "mozIStorageCompletionCallback.h"
23 #include "mozIStoragePendingStatement.h"
24 #include "mozilla/StaticPrefs_storage.h"
25 #include "mozilla/intl/Collator.h"
26 #include "mozilla/intl/LocaleService.h"
28 #include "sqlite3.h"
29 #include "mozilla/AutoSQLiteLifetime.h"
31 #ifdef XP_WIN
32 // "windows.h" was included and it can #define lots of things we care about...
33 # undef CompareString
34 #endif
36 using mozilla::intl::Collator;
38 namespace mozilla {
39 namespace storage {
41 ////////////////////////////////////////////////////////////////////////////////
42 //// Memory Reporting
44 #ifdef MOZ_DMD
45 mozilla::Atomic<size_t> gSqliteMemoryUsed;
46 #endif
48 static int64_t StorageSQLiteDistinguishedAmount() {
49 return ::sqlite3_memory_used();
52 /**
53 * Passes a single SQLite memory statistic to a memory reporter callback.
55 * @param aHandleReport
56 * The callback.
57 * @param aData
58 * The data for the callback.
59 * @param aConn
60 * The SQLite connection.
61 * @param aPathHead
62 * Head of the path for the memory report.
63 * @param aKind
64 * The memory report statistic kind, one of "stmt", "cache" or
65 * "schema".
66 * @param aDesc
67 * The memory report description.
68 * @param aOption
69 * The SQLite constant for getting the measurement.
70 * @param aTotal
71 * The accumulator for the measurement.
73 static void ReportConn(nsIHandleReportCallback* aHandleReport,
74 nsISupports* aData, Connection* aConn,
75 const nsACString& aPathHead, const nsACString& aKind,
76 const nsACString& aDesc, int32_t aOption,
77 size_t* aTotal) {
78 nsCString path(aPathHead);
79 path.Append(aKind);
80 path.AppendLiteral("-used");
82 int32_t val = aConn->getSqliteRuntimeStatus(aOption);
83 aHandleReport->Callback(""_ns, path, nsIMemoryReporter::KIND_HEAP,
84 nsIMemoryReporter::UNITS_BYTES, int64_t(val), aDesc,
85 aData);
86 *aTotal += val;
89 // Warning: To get a Connection's measurements requires holding its lock.
90 // There may be a delay getting the lock if another thread is accessing the
91 // Connection. This isn't very nice if CollectReports is called from the main
92 // thread! But at the time of writing this function is only called when
93 // about:memory is loaded (not, for example, when telemetry pings occur) and
94 // any delays in that case aren't so bad.
95 NS_IMETHODIMP
96 Service::CollectReports(nsIHandleReportCallback* aHandleReport,
97 nsISupports* aData, bool aAnonymize) {
98 size_t totalConnSize = 0;
100 nsTArray<RefPtr<Connection>> connections;
101 getConnections(connections);
103 for (uint32_t i = 0; i < connections.Length(); i++) {
104 RefPtr<Connection>& conn = connections[i];
106 // Someone may have closed the Connection, in which case we skip it.
107 // Note that we have consumers of the synchronous API that are off the
108 // main-thread, like the DOM Cache and IndexedDB, and as such we must be
109 // sure that we have a connection.
110 MutexAutoLock lockedAsyncScope(conn->sharedAsyncExecutionMutex);
111 if (!conn->connectionReady()) {
112 continue;
115 nsCString pathHead("explicit/storage/sqlite/");
116 // This filename isn't privacy-sensitive, and so is never anonymized.
117 pathHead.Append(conn->getFilename());
118 pathHead.Append('/');
120 SQLiteMutexAutoLock lockedScope(conn->sharedDBMutex);
122 constexpr auto stmtDesc =
123 "Memory (approximate) used by all prepared statements used by "
124 "connections to this database."_ns;
125 ReportConn(aHandleReport, aData, conn, pathHead, "stmt"_ns, stmtDesc,
126 SQLITE_DBSTATUS_STMT_USED, &totalConnSize);
128 constexpr auto cacheDesc =
129 "Memory (approximate) used by all pager caches used by connections "
130 "to this database."_ns;
131 ReportConn(aHandleReport, aData, conn, pathHead, "cache"_ns, cacheDesc,
132 SQLITE_DBSTATUS_CACHE_USED_SHARED, &totalConnSize);
134 constexpr auto schemaDesc =
135 "Memory (approximate) used to store the schema for all databases "
136 "associated with connections to this database."_ns;
137 ReportConn(aHandleReport, aData, conn, pathHead, "schema"_ns, schemaDesc,
138 SQLITE_DBSTATUS_SCHEMA_USED, &totalConnSize);
141 #ifdef MOZ_DMD
142 if (::sqlite3_memory_used() != int64_t(gSqliteMemoryUsed)) {
143 NS_WARNING(
144 "memory consumption reported by SQLite doesn't match "
145 "our measurements");
147 #endif
150 int64_t other = ::sqlite3_memory_used() - totalConnSize;
152 MOZ_COLLECT_REPORT("explicit/storage/sqlite/other", KIND_HEAP, UNITS_BYTES,
153 other, "All unclassified sqlite memory.");
155 return NS_OK;
158 ////////////////////////////////////////////////////////////////////////////////
159 //// Service
161 NS_IMPL_ISUPPORTS(Service, mozIStorageService, nsIObserver, nsIMemoryReporter)
163 Service* Service::gService = nullptr;
165 already_AddRefed<Service> Service::getSingleton() {
166 if (gService) {
167 return do_AddRef(gService);
170 // The first reference to the storage service must be obtained on the
171 // main thread.
172 NS_ENSURE_TRUE(NS_IsMainThread(), nullptr);
173 RefPtr<Service> service = new Service();
174 if (NS_SUCCEEDED(service->initialize())) {
175 // Note: This is cleared in the Service destructor.
176 gService = service.get();
177 return service.forget();
180 return nullptr;
183 int Service::AutoVFSRegistration::Init(UniquePtr<sqlite3_vfs> aVFS) {
184 MOZ_ASSERT(!mVFS);
185 if (aVFS) {
186 mVFS = std::move(aVFS);
187 return sqlite3_vfs_register(mVFS.get(), 0);
189 NS_WARNING("Failed to register VFS");
190 return SQLITE_OK;
193 Service::AutoVFSRegistration::~AutoVFSRegistration() {
194 if (mVFS) {
195 int rc = sqlite3_vfs_unregister(mVFS.get());
196 if (rc != SQLITE_OK) {
197 NS_WARNING("Failed to unregister sqlite vfs wrapper.");
202 Service::Service()
203 : mMutex("Service::mMutex"),
204 mRegistrationMutex("Service::mRegistrationMutex"),
205 mConnections() {}
207 Service::~Service() {
208 mozilla::UnregisterWeakMemoryReporter(this);
209 mozilla::UnregisterStorageSQLiteDistinguishedAmount();
211 gService = nullptr;
214 void Service::registerConnection(Connection* aConnection) {
215 mRegistrationMutex.AssertNotCurrentThreadOwns();
216 MutexAutoLock mutex(mRegistrationMutex);
217 (void)mConnections.AppendElement(aConnection);
220 void Service::unregisterConnection(Connection* aConnection) {
221 // If this is the last Connection it might be the only thing keeping Service
222 // alive. So ensure that Service is destroyed only after the Connection is
223 // cleanly unregistered and destroyed.
224 RefPtr<Service> kungFuDeathGrip(this);
225 RefPtr<Connection> forgettingRef;
227 mRegistrationMutex.AssertNotCurrentThreadOwns();
228 MutexAutoLock mutex(mRegistrationMutex);
230 for (uint32_t i = 0; i < mConnections.Length(); ++i) {
231 if (mConnections[i] == aConnection) {
232 // Because dropping the final reference can potentially result in
233 // spinning a nested event loop if the connection was not properly
234 // shutdown, we want to do that outside this loop so that we can finish
235 // mutating the array and drop our mutex.
236 forgettingRef = std::move(mConnections[i]);
237 mConnections.RemoveElementAt(i);
238 break;
243 MOZ_ASSERT(forgettingRef,
244 "Attempt to unregister unknown storage connection!");
246 // Do not proxy the release anywhere, just let this reference drop here. (We
247 // previously did proxy the release, but that was because we invoked Close()
248 // in the destructor and Close() likes to complain if it's not invoked on the
249 // opener event target, so it was essential that the last reference be dropped
250 // on the opener event target. We now enqueue Close() inside our caller,
251 // Release(), so it doesn't actually matter what thread our reference drops
252 // on.)
255 void Service::getConnections(
256 /* inout */ nsTArray<RefPtr<Connection>>& aConnections) {
257 mRegistrationMutex.AssertNotCurrentThreadOwns();
258 MutexAutoLock mutex(mRegistrationMutex);
259 aConnections.Clear();
260 aConnections.AppendElements(mConnections);
263 void Service::minimizeMemory() {
264 nsTArray<RefPtr<Connection>> connections;
265 getConnections(connections);
267 for (uint32_t i = 0; i < connections.Length(); i++) {
268 RefPtr<Connection> conn = connections[i];
269 // For non-main-thread owning/opening threads, we may be racing against them
270 // closing their connection or their thread. That's okay, see below.
271 if (!conn->connectionReady()) {
272 continue;
275 constexpr auto shrinkPragma = "PRAGMA shrink_memory"_ns;
277 if (!conn->operationSupported(Connection::SYNCHRONOUS)) {
278 // This is a mozIStorageAsyncConnection, it can only be used on the main
279 // thread, so we can do a straight API call.
280 nsCOMPtr<mozIStoragePendingStatement> ps;
281 DebugOnly<nsresult> rv = conn->ExecuteSimpleSQLAsync(
282 shrinkPragma, nullptr, getter_AddRefs(ps));
283 MOZ_ASSERT(NS_SUCCEEDED(rv), "Should have purged sqlite caches");
284 } else if (IsOnCurrentSerialEventTarget(conn->eventTargetOpenedOn)) {
285 if (conn->isAsyncExecutionThreadAvailable()) {
286 nsCOMPtr<mozIStoragePendingStatement> ps;
287 DebugOnly<nsresult> rv = conn->ExecuteSimpleSQLAsync(
288 shrinkPragma, nullptr, getter_AddRefs(ps));
289 MOZ_ASSERT(NS_SUCCEEDED(rv), "Should have purged sqlite caches");
290 } else {
291 conn->ExecuteSimpleSQL(shrinkPragma);
293 } else {
294 // We are on the wrong event target, the query should be executed on the
295 // opener event target, so we must dispatch to it.
296 // It's possible the connection is already closed or will be closed by the
297 // time our runnable runs. ExecuteSimpleSQL will safely return with a
298 // failure in that case. If the event target is shutting down or shut
299 // down, the dispatch will fail and that's okay.
300 nsCOMPtr<nsIRunnable> event = NewRunnableMethod<const nsCString>(
301 "Connection::ExecuteSimpleSQL", conn, &Connection::ExecuteSimpleSQL,
302 shrinkPragma);
303 Unused << conn->eventTargetOpenedOn->Dispatch(event, NS_DISPATCH_NORMAL);
308 UniquePtr<sqlite3_vfs> ConstructTelemetryVFS(bool);
309 const char* GetTelemetryVFSName(bool);
311 UniquePtr<sqlite3_vfs> ConstructObfuscatingVFS(const char* aBaseVFSName);
313 static const char* sObserverTopics[] = {"memory-pressure",
314 "xpcom-shutdown-threads"};
316 nsresult Service::initialize() {
317 MOZ_ASSERT(NS_IsMainThread(), "Must be initialized on the main thread");
319 int rc = AutoSQLiteLifetime::getInitResult();
320 if (rc != SQLITE_OK) {
321 return convertResultCode(rc);
324 rc = mTelemetrySqliteVFS.Init(ConstructTelemetryVFS(false));
325 if (rc != SQLITE_OK) {
326 return convertResultCode(rc);
329 rc = mTelemetryExclSqliteVFS.Init(ConstructTelemetryVFS(true));
330 if (rc != SQLITE_OK) {
331 return convertResultCode(rc);
334 rc = mObfuscatingSqliteVFS.Init(ConstructObfuscatingVFS(GetTelemetryVFSName(
335 StaticPrefs::storage_sqlite_exclusiveLock_enabled())));
336 if (rc != SQLITE_OK) {
337 return convertResultCode(rc);
340 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
341 NS_ENSURE_TRUE(os, NS_ERROR_FAILURE);
343 for (size_t i = 0; i < ArrayLength(sObserverTopics); ++i) {
344 nsresult rv = os->AddObserver(this, sObserverTopics[i], false);
345 if (NS_WARN_IF(NS_FAILED(rv))) {
346 return rv;
350 mozilla::RegisterWeakMemoryReporter(this);
351 mozilla::RegisterStorageSQLiteDistinguishedAmount(
352 StorageSQLiteDistinguishedAmount);
354 return NS_OK;
357 int Service::localeCompareStrings(const nsAString& aStr1,
358 const nsAString& aStr2,
359 Collator::Sensitivity aSensitivity) {
360 // The mozilla::intl::Collator is not thread safe, since the Collator::Options
361 // can be changed.
362 MutexAutoLock mutex(mMutex);
364 Collator* collator = getCollator();
365 if (!collator) {
366 NS_ERROR("Storage service has no collation");
367 return 0;
370 if (aSensitivity != mLastSensitivity) {
371 Collator::Options options{};
372 options.sensitivity = aSensitivity;
373 auto result = mCollator->SetOptions(options);
375 if (result.isErr()) {
376 NS_WARNING("Could not configure the mozilla::intl::Collation.");
377 return 0;
379 mLastSensitivity = aSensitivity;
382 return collator->CompareStrings(aStr1, aStr2);
385 Collator* Service::getCollator() {
386 mMutex.AssertCurrentThreadOwns();
388 if (mCollator) {
389 return mCollator.get();
392 auto result = mozilla::intl::LocaleService::TryCreateComponent<Collator>();
393 if (result.isErr()) {
394 NS_WARNING("Could not create mozilla::intl::Collation.");
395 return nullptr;
398 mCollator = result.unwrap();
400 // Sort in a case-insensitive way, where "base" letters are considered
401 // equal, e.g: a = á, a = A, a ≠ b.
402 Collator::Options options{};
403 options.sensitivity = Collator::Sensitivity::Base;
404 auto optResult = mCollator->SetOptions(options);
406 if (optResult.isErr()) {
407 NS_WARNING("Could not configure the mozilla::intl::Collation.");
408 mCollator = nullptr;
409 return nullptr;
412 return mCollator.get();
415 ////////////////////////////////////////////////////////////////////////////////
416 //// mozIStorageService
418 NS_IMETHODIMP
419 Service::OpenSpecialDatabase(const nsACString& aStorageKey,
420 const nsACString& aName, uint32_t aConnectionFlags,
421 mozIStorageConnection** _connection) {
422 if (!aStorageKey.Equals(kMozStorageMemoryStorageKey)) {
423 return NS_ERROR_INVALID_ARG;
426 const bool interruptible =
427 aConnectionFlags & mozIStorageService::CONNECTION_INTERRUPTIBLE;
429 int flags = SQLITE_OPEN_READWRITE;
431 if (!aName.IsEmpty()) {
432 flags |= SQLITE_OPEN_URI;
435 RefPtr<Connection> msc =
436 new Connection(this, flags, Connection::SYNCHRONOUS, interruptible);
438 const nsresult rv = msc->initialize(aStorageKey, aName);
439 NS_ENSURE_SUCCESS(rv, rv);
441 msc.forget(_connection);
442 return NS_OK;
445 namespace {
447 class AsyncInitDatabase final : public Runnable {
448 public:
449 AsyncInitDatabase(Connection* aConnection, nsIFile* aStorageFile,
450 int32_t aGrowthIncrement,
451 mozIStorageCompletionCallback* aCallback)
452 : Runnable("storage::AsyncInitDatabase"),
453 mConnection(aConnection),
454 mStorageFile(aStorageFile),
455 mGrowthIncrement(aGrowthIncrement),
456 mCallback(aCallback) {
457 MOZ_ASSERT(NS_IsMainThread());
460 NS_IMETHOD Run() override {
461 MOZ_ASSERT(!NS_IsMainThread());
462 nsresult rv = mConnection->initializeOnAsyncThread(mStorageFile);
463 if (NS_FAILED(rv)) {
464 return DispatchResult(rv, nullptr);
467 if (mGrowthIncrement >= 0) {
468 // Ignore errors. In the future, we might wish to log them.
469 (void)mConnection->SetGrowthIncrement(mGrowthIncrement, ""_ns);
472 return DispatchResult(
473 NS_OK, NS_ISUPPORTS_CAST(mozIStorageAsyncConnection*, mConnection));
476 private:
477 nsresult DispatchResult(nsresult aStatus, nsISupports* aValue) {
478 RefPtr<CallbackComplete> event =
479 new CallbackComplete(aStatus, aValue, mCallback.forget());
480 return NS_DispatchToMainThread(event);
483 ~AsyncInitDatabase() {
484 NS_ReleaseOnMainThread("AsyncInitDatabase::mStorageFile",
485 mStorageFile.forget());
486 NS_ReleaseOnMainThread("AsyncInitDatabase::mConnection",
487 mConnection.forget());
489 // Generally, the callback will be released by CallbackComplete.
490 // However, if for some reason Run() is not executed, we still
491 // need to ensure that it is released here.
492 NS_ReleaseOnMainThread("AsyncInitDatabase::mCallback", mCallback.forget());
495 RefPtr<Connection> mConnection;
496 nsCOMPtr<nsIFile> mStorageFile;
497 int32_t mGrowthIncrement;
498 RefPtr<mozIStorageCompletionCallback> mCallback;
501 } // namespace
503 NS_IMETHODIMP
504 Service::OpenAsyncDatabase(nsIVariant* aDatabaseStore, uint32_t aOpenFlags,
505 uint32_t /* aConnectionFlags */,
506 mozIStorageCompletionCallback* aCallback) {
507 if (!NS_IsMainThread()) {
508 return NS_ERROR_NOT_SAME_THREAD;
510 NS_ENSURE_ARG(aDatabaseStore);
511 NS_ENSURE_ARG(aCallback);
513 const bool shared = aOpenFlags & mozIStorageService::OPEN_SHARED;
514 const bool ignoreLockingMode =
515 aOpenFlags & mozIStorageService::OPEN_IGNORE_LOCKING_MODE;
516 // Specifying ignoreLockingMode will force use of the readOnly flag:
517 const bool readOnly =
518 ignoreLockingMode || (aOpenFlags & mozIStorageService::OPEN_READONLY);
519 int flags = readOnly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
521 nsCOMPtr<nsIFile> storageFile;
522 nsCOMPtr<nsISupports> dbStore;
523 nsresult rv = aDatabaseStore->GetAsISupports(getter_AddRefs(dbStore));
524 if (NS_SUCCEEDED(rv)) {
525 // Generally, aDatabaseStore holds the database nsIFile.
526 storageFile = do_QueryInterface(dbStore, &rv);
527 if (NS_FAILED(rv)) {
528 return NS_ERROR_INVALID_ARG;
531 nsCOMPtr<nsIFile> cloned;
532 rv = storageFile->Clone(getter_AddRefs(cloned));
533 MOZ_ASSERT(NS_SUCCEEDED(rv));
534 storageFile = std::move(cloned);
536 if (!readOnly) {
537 // Ensure that SQLITE_OPEN_CREATE is passed in for compatibility reasons.
538 flags |= SQLITE_OPEN_CREATE;
541 // Apply the shared-cache option.
542 flags |= shared ? SQLITE_OPEN_SHAREDCACHE : SQLITE_OPEN_PRIVATECACHE;
543 } else {
544 // Sometimes, however, it's a special database name.
545 nsAutoCString keyString;
546 rv = aDatabaseStore->GetAsACString(keyString);
547 if (NS_FAILED(rv) || !keyString.Equals(kMozStorageMemoryStorageKey)) {
548 return NS_ERROR_INVALID_ARG;
551 // Just fall through with nullptr storageFile, this will cause the storage
552 // connection to use a memory DB.
555 // Create connection on this thread, but initialize it on its helper thread.
556 RefPtr<Connection> msc =
557 new Connection(this, flags, Connection::ASYNCHRONOUS,
558 /* interruptible */ true, ignoreLockingMode);
559 nsCOMPtr<nsIEventTarget> target = msc->getAsyncExecutionTarget();
560 MOZ_ASSERT(target,
561 "Cannot initialize a connection that has been closed already");
563 RefPtr<AsyncInitDatabase> asyncInit = new AsyncInitDatabase(
564 msc, storageFile, /* growthIncrement */ -1, aCallback);
565 return target->Dispatch(asyncInit, nsIEventTarget::DISPATCH_NORMAL);
568 NS_IMETHODIMP
569 Service::OpenDatabase(nsIFile* aDatabaseFile, uint32_t aConnectionFlags,
570 mozIStorageConnection** _connection) {
571 NS_ENSURE_ARG(aDatabaseFile);
573 const bool interruptible =
574 aConnectionFlags & mozIStorageService::CONNECTION_INTERRUPTIBLE;
576 // Always ensure that SQLITE_OPEN_CREATE is passed in for compatibility
577 // reasons.
578 const int flags =
579 SQLITE_OPEN_READWRITE | SQLITE_OPEN_SHAREDCACHE | SQLITE_OPEN_CREATE;
580 RefPtr<Connection> msc =
581 new Connection(this, flags, Connection::SYNCHRONOUS, interruptible);
583 const nsresult rv = msc->initialize(aDatabaseFile);
584 NS_ENSURE_SUCCESS(rv, rv);
586 msc.forget(_connection);
587 return NS_OK;
590 NS_IMETHODIMP
591 Service::OpenUnsharedDatabase(nsIFile* aDatabaseFile, uint32_t aConnectionFlags,
592 mozIStorageConnection** _connection) {
593 NS_ENSURE_ARG(aDatabaseFile);
595 const bool interruptible =
596 aConnectionFlags & mozIStorageService::CONNECTION_INTERRUPTIBLE;
598 // Always ensure that SQLITE_OPEN_CREATE is passed in for compatibility
599 // reasons.
600 const int flags =
601 SQLITE_OPEN_READWRITE | SQLITE_OPEN_PRIVATECACHE | SQLITE_OPEN_CREATE;
602 RefPtr<Connection> msc =
603 new Connection(this, flags, Connection::SYNCHRONOUS, interruptible);
605 const nsresult rv = msc->initialize(aDatabaseFile);
606 NS_ENSURE_SUCCESS(rv, rv);
608 msc.forget(_connection);
609 return NS_OK;
612 NS_IMETHODIMP
613 Service::OpenDatabaseWithFileURL(nsIFileURL* aFileURL,
614 const nsACString& aTelemetryFilename,
615 uint32_t aConnectionFlags,
616 mozIStorageConnection** _connection) {
617 NS_ENSURE_ARG(aFileURL);
619 const bool interruptible =
620 aConnectionFlags & mozIStorageService::CONNECTION_INTERRUPTIBLE;
622 // Always ensure that SQLITE_OPEN_CREATE is passed in for compatibility
623 // reasons.
624 const int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_SHAREDCACHE |
625 SQLITE_OPEN_CREATE | SQLITE_OPEN_URI;
626 RefPtr<Connection> msc =
627 new Connection(this, flags, Connection::SYNCHRONOUS, interruptible);
629 const nsresult rv = msc->initialize(aFileURL, aTelemetryFilename);
630 NS_ENSURE_SUCCESS(rv, rv);
632 msc.forget(_connection);
633 return NS_OK;
636 NS_IMETHODIMP
637 Service::BackupDatabaseFile(nsIFile* aDBFile, const nsAString& aBackupFileName,
638 nsIFile* aBackupParentDirectory, nsIFile** backup) {
639 nsresult rv;
640 nsCOMPtr<nsIFile> parentDir = aBackupParentDirectory;
641 if (!parentDir) {
642 // This argument is optional, and defaults to the same parent directory
643 // as the current file.
644 rv = aDBFile->GetParent(getter_AddRefs(parentDir));
645 NS_ENSURE_SUCCESS(rv, rv);
648 nsCOMPtr<nsIFile> backupDB;
649 rv = parentDir->Clone(getter_AddRefs(backupDB));
650 NS_ENSURE_SUCCESS(rv, rv);
652 rv = backupDB->Append(aBackupFileName);
653 NS_ENSURE_SUCCESS(rv, rv);
655 rv = backupDB->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
656 NS_ENSURE_SUCCESS(rv, rv);
658 nsAutoString fileName;
659 rv = backupDB->GetLeafName(fileName);
660 NS_ENSURE_SUCCESS(rv, rv);
662 rv = backupDB->Remove(false);
663 NS_ENSURE_SUCCESS(rv, rv);
665 backupDB.forget(backup);
667 return aDBFile->CopyTo(parentDir, fileName);
670 ////////////////////////////////////////////////////////////////////////////////
671 //// nsIObserver
673 NS_IMETHODIMP
674 Service::Observe(nsISupports*, const char* aTopic, const char16_t*) {
675 if (strcmp(aTopic, "memory-pressure") == 0) {
676 minimizeMemory();
677 } else if (strcmp(aTopic, "xpcom-shutdown-threads") == 0) {
678 // The Service is kept alive by our strong observer references and
679 // references held by Connection instances. Since we're about to remove the
680 // former and then wait for the latter ones to go away, it behooves us to
681 // hold a strong reference to ourselves so our calls to getConnections() do
682 // not happen on a deleted object.
683 RefPtr<Service> kungFuDeathGrip = this;
685 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
687 for (size_t i = 0; i < ArrayLength(sObserverTopics); ++i) {
688 (void)os->RemoveObserver(this, sObserverTopics[i]);
691 SpinEventLoopUntil("storage::Service::Observe(xpcom-shutdown-threads)"_ns,
692 [&]() -> bool {
693 // We must wait until all the closing connections are
694 // closed.
695 nsTArray<RefPtr<Connection>> connections;
696 getConnections(connections);
697 for (auto& conn : connections) {
698 if (conn->isClosing()) {
699 return false;
702 return true;
705 #ifdef DEBUG
706 nsTArray<RefPtr<Connection>> connections;
707 getConnections(connections);
708 for (uint32_t i = 0, n = connections.Length(); i < n; i++) {
709 if (!connections[i]->isClosed()) {
710 // getFilename is only the leaf name for the database file,
711 // so it shouldn't contain privacy-sensitive information.
712 CrashReporter::AnnotateCrashReport(
713 CrashReporter::Annotation::StorageConnectionNotClosed,
714 connections[i]->getFilename());
715 printf_stderr("Storage connection not closed: %s",
716 connections[i]->getFilename().get());
717 MOZ_CRASH();
720 #endif
723 return NS_OK;
726 } // namespace storage
727 } // namespace mozilla