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"
29 #include "mozilla/AutoSQLiteLifetime.h"
32 // "windows.h" was included and it can #define lots of things we care about...
36 using mozilla::intl::Collator
;
41 ////////////////////////////////////////////////////////////////////////////////
45 mozilla::Atomic
<size_t> gSqliteMemoryUsed
;
48 static int64_t StorageSQLiteDistinguishedAmount() {
49 return ::sqlite3_memory_used();
53 * Passes a single SQLite memory statistic to a memory reporter callback.
55 * @param aHandleReport
58 * The data for the callback.
60 * The SQLite connection.
62 * Head of the path for the memory report.
64 * The memory report statistic kind, one of "stmt", "cache" or
67 * The memory report description.
69 * The SQLite constant for getting the measurement.
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
,
78 nsCString
path(aPathHead
);
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
,
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.
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()) {
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
);
142 if (::sqlite3_memory_used() != int64_t(gSqliteMemoryUsed
)) {
144 "memory consumption reported by SQLite doesn't match "
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.");
158 ////////////////////////////////////////////////////////////////////////////////
161 NS_IMPL_ISUPPORTS(Service
, mozIStorageService
, nsIObserver
, nsIMemoryReporter
)
163 Service
* Service::gService
= nullptr;
165 already_AddRefed
<Service
> Service::getSingleton() {
167 return do_AddRef(gService
);
170 // The first reference to the storage service must be obtained on the
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();
183 int Service::AutoVFSRegistration::Init(UniquePtr
<sqlite3_vfs
> aVFS
) {
186 mVFS
= std::move(aVFS
);
187 return sqlite3_vfs_register(mVFS
.get(), 0);
189 NS_WARNING("Failed to register VFS");
193 Service::AutoVFSRegistration::~AutoVFSRegistration() {
195 int rc
= sqlite3_vfs_unregister(mVFS
.get());
196 if (rc
!= SQLITE_OK
) {
197 NS_WARNING("Failed to unregister sqlite vfs wrapper.");
203 : mMutex("Service::mMutex"),
204 mRegistrationMutex("Service::mRegistrationMutex"),
207 Service::~Service() {
208 mozilla::UnregisterWeakMemoryReporter(this);
209 mozilla::UnregisterStorageSQLiteDistinguishedAmount();
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
);
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
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()) {
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");
291 conn
->ExecuteSimpleSQL(shrinkPragma
);
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
,
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 UniquePtr
<sqlite3_vfs
> ConstructReadOnlyNoLockVFS();
315 static const char* sObserverTopics
[] = {"memory-pressure",
316 "xpcom-shutdown-threads"};
318 nsresult
Service::initialize() {
319 MOZ_ASSERT(NS_IsMainThread(), "Must be initialized on the main thread");
321 int rc
= AutoSQLiteLifetime::getInitResult();
322 if (rc
!= SQLITE_OK
) {
323 return convertResultCode(rc
);
326 rc
= mTelemetrySqliteVFS
.Init(ConstructTelemetryVFS(false));
327 if (rc
!= SQLITE_OK
) {
328 return convertResultCode(rc
);
331 rc
= mTelemetryExclSqliteVFS
.Init(ConstructTelemetryVFS(true));
332 if (rc
!= SQLITE_OK
) {
333 return convertResultCode(rc
);
336 rc
= mObfuscatingSqliteVFS
.Init(ConstructObfuscatingVFS(GetTelemetryVFSName(
337 StaticPrefs::storage_sqlite_exclusiveLock_enabled())));
338 if (rc
!= SQLITE_OK
) {
339 return convertResultCode(rc
);
342 rc
= mReadOnlyNoLockSqliteVFS
.Init(ConstructReadOnlyNoLockVFS());
343 if (rc
!= SQLITE_OK
) {
344 return convertResultCode(rc
);
347 nsCOMPtr
<nsIObserverService
> os
= mozilla::services::GetObserverService();
348 NS_ENSURE_TRUE(os
, NS_ERROR_FAILURE
);
350 for (size_t i
= 0; i
< ArrayLength(sObserverTopics
); ++i
) {
351 nsresult rv
= os
->AddObserver(this, sObserverTopics
[i
], false);
352 if (NS_WARN_IF(NS_FAILED(rv
))) {
357 mozilla::RegisterWeakMemoryReporter(this);
358 mozilla::RegisterStorageSQLiteDistinguishedAmount(
359 StorageSQLiteDistinguishedAmount
);
364 int Service::localeCompareStrings(const nsAString
& aStr1
,
365 const nsAString
& aStr2
,
366 Collator::Sensitivity aSensitivity
) {
367 // The mozilla::intl::Collator is not thread safe, since the Collator::Options
369 MutexAutoLock
mutex(mMutex
);
371 Collator
* collator
= getCollator();
373 NS_ERROR("Storage service has no collation");
377 if (aSensitivity
!= mLastSensitivity
) {
378 Collator::Options options
{};
379 options
.sensitivity
= aSensitivity
;
380 auto result
= mCollator
->SetOptions(options
);
382 if (result
.isErr()) {
383 NS_WARNING("Could not configure the mozilla::intl::Collation.");
386 mLastSensitivity
= aSensitivity
;
389 return collator
->CompareStrings(aStr1
, aStr2
);
392 Collator
* Service::getCollator() {
393 mMutex
.AssertCurrentThreadOwns();
396 return mCollator
.get();
399 auto result
= mozilla::intl::LocaleService::TryCreateComponent
<Collator
>();
400 if (result
.isErr()) {
401 NS_WARNING("Could not create mozilla::intl::Collation.");
405 mCollator
= result
.unwrap();
407 // Sort in a case-insensitive way, where "base" letters are considered
408 // equal, e.g: a = á, a = A, a ≠b.
409 Collator::Options options
{};
410 options
.sensitivity
= Collator::Sensitivity::Base
;
411 auto optResult
= mCollator
->SetOptions(options
);
413 if (optResult
.isErr()) {
414 NS_WARNING("Could not configure the mozilla::intl::Collation.");
419 return mCollator
.get();
422 ////////////////////////////////////////////////////////////////////////////////
423 //// mozIStorageService
426 Service::OpenSpecialDatabase(const nsACString
& aStorageKey
,
427 const nsACString
& aName
, uint32_t aConnectionFlags
,
428 mozIStorageConnection
** _connection
) {
429 if (!aStorageKey
.Equals(kMozStorageMemoryStorageKey
)) {
430 return NS_ERROR_INVALID_ARG
;
433 const bool interruptible
=
434 aConnectionFlags
& mozIStorageService::CONNECTION_INTERRUPTIBLE
;
436 int flags
= SQLITE_OPEN_READWRITE
;
438 if (!aName
.IsEmpty()) {
439 flags
|= SQLITE_OPEN_URI
;
442 RefPtr
<Connection
> msc
=
443 new Connection(this, flags
, Connection::SYNCHRONOUS
, interruptible
);
445 const nsresult rv
= msc
->initialize(aStorageKey
, aName
);
446 NS_ENSURE_SUCCESS(rv
, rv
);
448 msc
.forget(_connection
);
454 class AsyncInitDatabase final
: public Runnable
{
456 AsyncInitDatabase(Connection
* aConnection
, nsIFile
* aStorageFile
,
457 int32_t aGrowthIncrement
,
458 mozIStorageCompletionCallback
* aCallback
)
459 : Runnable("storage::AsyncInitDatabase"),
460 mConnection(aConnection
),
461 mStorageFile(aStorageFile
),
462 mGrowthIncrement(aGrowthIncrement
),
463 mCallback(aCallback
) {
464 MOZ_ASSERT(NS_IsMainThread());
467 NS_IMETHOD
Run() override
{
468 MOZ_ASSERT(!NS_IsMainThread());
469 nsresult rv
= mConnection
->initializeOnAsyncThread(mStorageFile
);
471 return DispatchResult(rv
, nullptr);
474 if (mGrowthIncrement
>= 0) {
475 // Ignore errors. In the future, we might wish to log them.
476 (void)mConnection
->SetGrowthIncrement(mGrowthIncrement
, ""_ns
);
479 return DispatchResult(
480 NS_OK
, NS_ISUPPORTS_CAST(mozIStorageAsyncConnection
*, mConnection
));
484 nsresult
DispatchResult(nsresult aStatus
, nsISupports
* aValue
) {
485 RefPtr
<CallbackComplete
> event
=
486 new CallbackComplete(aStatus
, aValue
, mCallback
.forget());
487 return NS_DispatchToMainThread(event
);
490 ~AsyncInitDatabase() {
491 NS_ReleaseOnMainThread("AsyncInitDatabase::mStorageFile",
492 mStorageFile
.forget());
493 NS_ReleaseOnMainThread("AsyncInitDatabase::mConnection",
494 mConnection
.forget());
496 // Generally, the callback will be released by CallbackComplete.
497 // However, if for some reason Run() is not executed, we still
498 // need to ensure that it is released here.
499 NS_ReleaseOnMainThread("AsyncInitDatabase::mCallback", mCallback
.forget());
502 RefPtr
<Connection
> mConnection
;
503 nsCOMPtr
<nsIFile
> mStorageFile
;
504 int32_t mGrowthIncrement
;
505 RefPtr
<mozIStorageCompletionCallback
> mCallback
;
511 Service::OpenAsyncDatabase(nsIVariant
* aDatabaseStore
, uint32_t aOpenFlags
,
512 uint32_t /* aConnectionFlags */,
513 mozIStorageCompletionCallback
* aCallback
) {
514 if (!NS_IsMainThread()) {
515 return NS_ERROR_NOT_SAME_THREAD
;
517 NS_ENSURE_ARG(aDatabaseStore
);
518 NS_ENSURE_ARG(aCallback
);
520 const bool shared
= aOpenFlags
& mozIStorageService::OPEN_SHARED
;
521 const bool ignoreLockingMode
=
522 aOpenFlags
& mozIStorageService::OPEN_IGNORE_LOCKING_MODE
;
523 // Specifying ignoreLockingMode will force use of the readOnly flag:
524 const bool readOnly
=
525 ignoreLockingMode
|| (aOpenFlags
& mozIStorageService::OPEN_READONLY
);
526 int flags
= readOnly
? SQLITE_OPEN_READONLY
: SQLITE_OPEN_READWRITE
;
528 nsCOMPtr
<nsIFile
> storageFile
;
529 nsCOMPtr
<nsISupports
> dbStore
;
530 nsresult rv
= aDatabaseStore
->GetAsISupports(getter_AddRefs(dbStore
));
531 if (NS_SUCCEEDED(rv
)) {
532 // Generally, aDatabaseStore holds the database nsIFile.
533 storageFile
= do_QueryInterface(dbStore
, &rv
);
535 return NS_ERROR_INVALID_ARG
;
538 nsCOMPtr
<nsIFile
> cloned
;
539 rv
= storageFile
->Clone(getter_AddRefs(cloned
));
540 MOZ_ASSERT(NS_SUCCEEDED(rv
));
541 storageFile
= std::move(cloned
);
544 // Ensure that SQLITE_OPEN_CREATE is passed in for compatibility reasons.
545 flags
|= SQLITE_OPEN_CREATE
;
548 // Apply the shared-cache option.
549 flags
|= shared
? SQLITE_OPEN_SHAREDCACHE
: SQLITE_OPEN_PRIVATECACHE
;
551 // Sometimes, however, it's a special database name.
552 nsAutoCString keyString
;
553 rv
= aDatabaseStore
->GetAsACString(keyString
);
554 if (NS_FAILED(rv
) || !keyString
.Equals(kMozStorageMemoryStorageKey
)) {
555 return NS_ERROR_INVALID_ARG
;
558 // Just fall through with nullptr storageFile, this will cause the storage
559 // connection to use a memory DB.
562 // Create connection on this thread, but initialize it on its helper thread.
563 RefPtr
<Connection
> msc
=
564 new Connection(this, flags
, Connection::ASYNCHRONOUS
,
565 /* interruptible */ true, ignoreLockingMode
);
566 nsCOMPtr
<nsIEventTarget
> target
= msc
->getAsyncExecutionTarget();
568 "Cannot initialize a connection that has been closed already");
570 RefPtr
<AsyncInitDatabase
> asyncInit
= new AsyncInitDatabase(
571 msc
, storageFile
, /* growthIncrement */ -1, aCallback
);
572 return target
->Dispatch(asyncInit
, nsIEventTarget::DISPATCH_NORMAL
);
576 Service::OpenDatabase(nsIFile
* aDatabaseFile
, uint32_t aConnectionFlags
,
577 mozIStorageConnection
** _connection
) {
578 NS_ENSURE_ARG(aDatabaseFile
);
580 const bool interruptible
=
581 aConnectionFlags
& mozIStorageService::CONNECTION_INTERRUPTIBLE
;
583 // Always ensure that SQLITE_OPEN_CREATE is passed in for compatibility
586 SQLITE_OPEN_READWRITE
| SQLITE_OPEN_SHAREDCACHE
| SQLITE_OPEN_CREATE
;
587 RefPtr
<Connection
> msc
=
588 new Connection(this, flags
, Connection::SYNCHRONOUS
, interruptible
);
590 const nsresult rv
= msc
->initialize(aDatabaseFile
);
591 NS_ENSURE_SUCCESS(rv
, rv
);
593 msc
.forget(_connection
);
598 Service::OpenUnsharedDatabase(nsIFile
* aDatabaseFile
, uint32_t aConnectionFlags
,
599 mozIStorageConnection
** _connection
) {
600 NS_ENSURE_ARG(aDatabaseFile
);
602 const bool interruptible
=
603 aConnectionFlags
& mozIStorageService::CONNECTION_INTERRUPTIBLE
;
605 // Always ensure that SQLITE_OPEN_CREATE is passed in for compatibility
608 SQLITE_OPEN_READWRITE
| SQLITE_OPEN_PRIVATECACHE
| SQLITE_OPEN_CREATE
;
609 RefPtr
<Connection
> msc
=
610 new Connection(this, flags
, Connection::SYNCHRONOUS
, interruptible
);
612 const nsresult rv
= msc
->initialize(aDatabaseFile
);
613 NS_ENSURE_SUCCESS(rv
, rv
);
615 msc
.forget(_connection
);
620 Service::OpenDatabaseWithFileURL(nsIFileURL
* aFileURL
,
621 const nsACString
& aTelemetryFilename
,
622 uint32_t aConnectionFlags
,
623 mozIStorageConnection
** _connection
) {
624 NS_ENSURE_ARG(aFileURL
);
626 const bool interruptible
=
627 aConnectionFlags
& mozIStorageService::CONNECTION_INTERRUPTIBLE
;
629 // Always ensure that SQLITE_OPEN_CREATE is passed in for compatibility
631 const int flags
= SQLITE_OPEN_READWRITE
| SQLITE_OPEN_SHAREDCACHE
|
632 SQLITE_OPEN_CREATE
| SQLITE_OPEN_URI
;
633 RefPtr
<Connection
> msc
=
634 new Connection(this, flags
, Connection::SYNCHRONOUS
, interruptible
);
636 const nsresult rv
= msc
->initialize(aFileURL
, aTelemetryFilename
);
637 NS_ENSURE_SUCCESS(rv
, rv
);
639 msc
.forget(_connection
);
644 Service::BackupDatabaseFile(nsIFile
* aDBFile
, const nsAString
& aBackupFileName
,
645 nsIFile
* aBackupParentDirectory
, nsIFile
** backup
) {
647 nsCOMPtr
<nsIFile
> parentDir
= aBackupParentDirectory
;
649 // This argument is optional, and defaults to the same parent directory
650 // as the current file.
651 rv
= aDBFile
->GetParent(getter_AddRefs(parentDir
));
652 NS_ENSURE_SUCCESS(rv
, rv
);
655 nsCOMPtr
<nsIFile
> backupDB
;
656 rv
= parentDir
->Clone(getter_AddRefs(backupDB
));
657 NS_ENSURE_SUCCESS(rv
, rv
);
659 rv
= backupDB
->Append(aBackupFileName
);
660 NS_ENSURE_SUCCESS(rv
, rv
);
662 rv
= backupDB
->CreateUnique(nsIFile::NORMAL_FILE_TYPE
, 0600);
663 NS_ENSURE_SUCCESS(rv
, rv
);
665 nsAutoString fileName
;
666 rv
= backupDB
->GetLeafName(fileName
);
667 NS_ENSURE_SUCCESS(rv
, rv
);
669 rv
= backupDB
->Remove(false);
670 NS_ENSURE_SUCCESS(rv
, rv
);
672 backupDB
.forget(backup
);
674 return aDBFile
->CopyTo(parentDir
, fileName
);
677 ////////////////////////////////////////////////////////////////////////////////
681 Service::Observe(nsISupports
*, const char* aTopic
, const char16_t
*) {
682 if (strcmp(aTopic
, "memory-pressure") == 0) {
684 } else if (strcmp(aTopic
, "xpcom-shutdown-threads") == 0) {
685 // The Service is kept alive by our strong observer references and
686 // references held by Connection instances. Since we're about to remove the
687 // former and then wait for the latter ones to go away, it behooves us to
688 // hold a strong reference to ourselves so our calls to getConnections() do
689 // not happen on a deleted object.
690 RefPtr
<Service
> kungFuDeathGrip
= this;
692 nsCOMPtr
<nsIObserverService
> os
= mozilla::services::GetObserverService();
694 for (size_t i
= 0; i
< ArrayLength(sObserverTopics
); ++i
) {
695 (void)os
->RemoveObserver(this, sObserverTopics
[i
]);
698 SpinEventLoopUntil("storage::Service::Observe(xpcom-shutdown-threads)"_ns
,
700 // We must wait until all the closing connections are
702 nsTArray
<RefPtr
<Connection
>> connections
;
703 getConnections(connections
);
704 for (auto& conn
: connections
) {
705 if (conn
->isClosing()) {
713 nsTArray
<RefPtr
<Connection
>> connections
;
714 getConnections(connections
);
715 for (uint32_t i
= 0, n
= connections
.Length(); i
< n
; i
++) {
716 if (!connections
[i
]->isClosed()) {
717 // getFilename is only the leaf name for the database file,
718 // so it shouldn't contain privacy-sensitive information.
719 CrashReporter::AnnotateCrashReport(
720 CrashReporter::Annotation::StorageConnectionNotClosed
,
721 connections
[i
]->getFilename());
722 printf_stderr("Storage connection not closed: %s",
723 connections
[i
]->getFilename().get());
733 } // namespace storage
734 } // namespace mozilla