Bug 1728955: part 7) Log result of Windows' `OleGetClipboardResult` . r=masayuki
[gecko.git] / storage / mozStorageService.cpp
bloba65ce9247b0855fe744e2d3e21f7e71f28cedc9b
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 thread, so it was essential that the last reference be dropped on
250 // the opener thread. We now enqueue Close() inside our caller, Release(), so
251 // it doesn't actually matter what thread our reference drops on.)
254 void Service::getConnections(
255 /* inout */ nsTArray<RefPtr<Connection>>& aConnections) {
256 mRegistrationMutex.AssertNotCurrentThreadOwns();
257 MutexAutoLock mutex(mRegistrationMutex);
258 aConnections.Clear();
259 aConnections.AppendElements(mConnections);
262 void Service::minimizeMemory() {
263 nsTArray<RefPtr<Connection>> connections;
264 getConnections(connections);
266 for (uint32_t i = 0; i < connections.Length(); i++) {
267 RefPtr<Connection> conn = connections[i];
268 // For non-main-thread owning/opening threads, we may be racing against them
269 // closing their connection or their thread. That's okay, see below.
270 if (!conn->connectionReady()) {
271 continue;
274 constexpr auto shrinkPragma = "PRAGMA shrink_memory"_ns;
275 bool onOpenedThread = false;
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 (NS_SUCCEEDED(
285 conn->threadOpenedOn->IsOnCurrentThread(&onOpenedThread)) &&
286 onOpenedThread) {
287 if (conn->isAsyncExecutionThreadAvailable()) {
288 nsCOMPtr<mozIStoragePendingStatement> ps;
289 DebugOnly<nsresult> rv = conn->ExecuteSimpleSQLAsync(
290 shrinkPragma, nullptr, getter_AddRefs(ps));
291 MOZ_ASSERT(NS_SUCCEEDED(rv), "Should have purged sqlite caches");
292 } else {
293 conn->ExecuteSimpleSQL(shrinkPragma);
295 } else {
296 // We are on the wrong thread, the query should be executed on the
297 // opener thread, so we must dispatch to it.
298 // It's possible the connection is already closed or will be closed by the
299 // time our runnable runs. ExecuteSimpleSQL will safely return with a
300 // failure in that case. If the thread is shutting down or shut down, the
301 // dispatch will fail and that's okay.
302 nsCOMPtr<nsIRunnable> event = NewRunnableMethod<const nsCString>(
303 "Connection::ExecuteSimpleSQL", conn, &Connection::ExecuteSimpleSQL,
304 shrinkPragma);
305 Unused << conn->threadOpenedOn->Dispatch(event, NS_DISPATCH_NORMAL);
310 UniquePtr<sqlite3_vfs> ConstructTelemetryVFS(bool);
311 const char* GetTelemetryVFSName(bool);
313 UniquePtr<sqlite3_vfs> ConstructObfuscatingVFS(const char* aBaseVFSName);
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 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
343 NS_ENSURE_TRUE(os, NS_ERROR_FAILURE);
345 for (size_t i = 0; i < ArrayLength(sObserverTopics); ++i) {
346 nsresult rv = os->AddObserver(this, sObserverTopics[i], false);
347 if (NS_WARN_IF(NS_FAILED(rv))) {
348 return rv;
352 mozilla::RegisterWeakMemoryReporter(this);
353 mozilla::RegisterStorageSQLiteDistinguishedAmount(
354 StorageSQLiteDistinguishedAmount);
356 return NS_OK;
359 int Service::localeCompareStrings(const nsAString& aStr1,
360 const nsAString& aStr2,
361 Collator::Sensitivity aSensitivity) {
362 // The mozilla::intl::Collator is not thread safe, since the Collator::Options
363 // can be changed.
364 MutexAutoLock mutex(mMutex);
366 Collator* collator = getCollator();
367 if (!collator) {
368 NS_ERROR("Storage service has no collation");
369 return 0;
372 if (aSensitivity != mLastSensitivity) {
373 Collator::Options options{};
374 options.sensitivity = aSensitivity;
375 auto result = mCollator->SetOptions(options);
377 if (result.isErr()) {
378 NS_WARNING("Could not configure the mozilla::intl::Collation.");
379 return 0;
381 mLastSensitivity = aSensitivity;
384 return collator->CompareStrings(aStr1, aStr2);
387 Collator* Service::getCollator() {
388 mMutex.AssertCurrentThreadOwns();
390 if (mCollator) {
391 return mCollator.get();
394 auto result = mozilla::intl::LocaleService::TryCreateComponent<Collator>();
395 if (result.isErr()) {
396 NS_WARNING("Could not create mozilla::intl::Collation.");
397 return nullptr;
400 mCollator = result.unwrap();
402 // Sort in a case-insensitive way, where "base" letters are considered
403 // equal, e.g: a = á, a = A, a ≠ b.
404 Collator::Options options{};
405 options.sensitivity = Collator::Sensitivity::Base;
406 auto optResult = mCollator->SetOptions(options);
408 if (optResult.isErr()) {
409 NS_WARNING("Could not configure the mozilla::intl::Collation.");
410 mCollator = nullptr;
411 return nullptr;
414 return mCollator.get();
417 ////////////////////////////////////////////////////////////////////////////////
418 //// mozIStorageService
420 NS_IMETHODIMP
421 Service::OpenSpecialDatabase(const nsACString& aStorageKey,
422 const nsACString& aName,
423 mozIStorageConnection** _connection) {
424 if (!aStorageKey.Equals(kMozStorageMemoryStorageKey)) {
425 return NS_ERROR_INVALID_ARG;
428 int flags = SQLITE_OPEN_READWRITE;
430 if (!aName.IsEmpty()) {
431 flags |= SQLITE_OPEN_URI;
434 RefPtr<Connection> msc = new Connection(this, flags, Connection::SYNCHRONOUS);
436 nsresult rv = msc->initialize(aStorageKey, aName);
437 NS_ENSURE_SUCCESS(rv, rv);
439 msc.forget(_connection);
440 return NS_OK;
443 namespace {
445 class AsyncInitDatabase final : public Runnable {
446 public:
447 AsyncInitDatabase(Connection* aConnection, nsIFile* aStorageFile,
448 int32_t aGrowthIncrement,
449 mozIStorageCompletionCallback* aCallback)
450 : Runnable("storage::AsyncInitDatabase"),
451 mConnection(aConnection),
452 mStorageFile(aStorageFile),
453 mGrowthIncrement(aGrowthIncrement),
454 mCallback(aCallback) {
455 MOZ_ASSERT(NS_IsMainThread());
458 NS_IMETHOD Run() override {
459 MOZ_ASSERT(!NS_IsMainThread());
460 nsresult rv = mConnection->initializeOnAsyncThread(mStorageFile);
461 if (NS_FAILED(rv)) {
462 return DispatchResult(rv, nullptr);
465 if (mGrowthIncrement >= 0) {
466 // Ignore errors. In the future, we might wish to log them.
467 (void)mConnection->SetGrowthIncrement(mGrowthIncrement, ""_ns);
470 return DispatchResult(
471 NS_OK, NS_ISUPPORTS_CAST(mozIStorageAsyncConnection*, mConnection));
474 private:
475 nsresult DispatchResult(nsresult aStatus, nsISupports* aValue) {
476 RefPtr<CallbackComplete> event =
477 new CallbackComplete(aStatus, aValue, mCallback.forget());
478 return NS_DispatchToMainThread(event);
481 ~AsyncInitDatabase() {
482 NS_ReleaseOnMainThread("AsyncInitDatabase::mStorageFile",
483 mStorageFile.forget());
484 NS_ReleaseOnMainThread("AsyncInitDatabase::mConnection",
485 mConnection.forget());
487 // Generally, the callback will be released by CallbackComplete.
488 // However, if for some reason Run() is not executed, we still
489 // need to ensure that it is released here.
490 NS_ReleaseOnMainThread("AsyncInitDatabase::mCallback", mCallback.forget());
493 RefPtr<Connection> mConnection;
494 nsCOMPtr<nsIFile> mStorageFile;
495 int32_t mGrowthIncrement;
496 RefPtr<mozIStorageCompletionCallback> mCallback;
499 } // namespace
501 NS_IMETHODIMP
502 Service::OpenAsyncDatabase(nsIVariant* aDatabaseStore,
503 nsIPropertyBag2* aOptions,
504 mozIStorageCompletionCallback* aCallback) {
505 if (!NS_IsMainThread()) {
506 return NS_ERROR_NOT_SAME_THREAD;
508 NS_ENSURE_ARG(aDatabaseStore);
509 NS_ENSURE_ARG(aCallback);
511 nsresult rv;
512 bool shared = false;
513 bool readOnly = false;
514 bool ignoreLockingMode = false;
515 int32_t growthIncrement = -1;
517 #define FAIL_IF_SET_BUT_INVALID(rv) \
518 if (NS_FAILED(rv) && rv != NS_ERROR_NOT_AVAILABLE) { \
519 return NS_ERROR_INVALID_ARG; \
522 // Deal with options first:
523 if (aOptions) {
524 rv = aOptions->GetPropertyAsBool(u"readOnly"_ns, &readOnly);
525 FAIL_IF_SET_BUT_INVALID(rv);
527 rv = aOptions->GetPropertyAsBool(u"ignoreLockingMode"_ns,
528 &ignoreLockingMode);
529 FAIL_IF_SET_BUT_INVALID(rv);
530 // Specifying ignoreLockingMode will force use of the readOnly flag:
531 if (ignoreLockingMode) {
532 readOnly = true;
535 rv = aOptions->GetPropertyAsBool(u"shared"_ns, &shared);
536 FAIL_IF_SET_BUT_INVALID(rv);
538 // NB: we re-set to -1 if we don't have a storage file later on.
539 rv = aOptions->GetPropertyAsInt32(u"growthIncrement"_ns, &growthIncrement);
540 FAIL_IF_SET_BUT_INVALID(rv);
542 int flags = readOnly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
544 nsCOMPtr<nsIFile> storageFile;
545 nsCOMPtr<nsISupports> dbStore;
546 rv = aDatabaseStore->GetAsISupports(getter_AddRefs(dbStore));
547 if (NS_SUCCEEDED(rv)) {
548 // Generally, aDatabaseStore holds the database nsIFile.
549 storageFile = do_QueryInterface(dbStore, &rv);
550 if (NS_FAILED(rv)) {
551 return NS_ERROR_INVALID_ARG;
554 nsCOMPtr<nsIFile> cloned;
555 rv = storageFile->Clone(getter_AddRefs(cloned));
556 MOZ_ASSERT(NS_SUCCEEDED(rv));
557 storageFile = std::move(cloned);
559 if (!readOnly) {
560 // Ensure that SQLITE_OPEN_CREATE is passed in for compatibility reasons.
561 flags |= SQLITE_OPEN_CREATE;
564 // Apply the shared-cache option.
565 flags |= shared ? SQLITE_OPEN_SHAREDCACHE : SQLITE_OPEN_PRIVATECACHE;
566 } else {
567 // Sometimes, however, it's a special database name.
568 nsAutoCString keyString;
569 rv = aDatabaseStore->GetAsACString(keyString);
570 if (NS_FAILED(rv) || !keyString.Equals(kMozStorageMemoryStorageKey)) {
571 return NS_ERROR_INVALID_ARG;
574 // Just fall through with nullptr storageFile, this will cause the storage
575 // connection to use a memory DB.
578 if (!storageFile && growthIncrement >= 0) {
579 return NS_ERROR_INVALID_ARG;
582 // Create connection on this thread, but initialize it on its helper thread.
583 RefPtr<Connection> msc =
584 new Connection(this, flags, Connection::ASYNCHRONOUS, ignoreLockingMode);
585 nsCOMPtr<nsIEventTarget> target = msc->getAsyncExecutionTarget();
586 MOZ_ASSERT(target,
587 "Cannot initialize a connection that has been closed already");
589 RefPtr<AsyncInitDatabase> asyncInit =
590 new AsyncInitDatabase(msc, storageFile, growthIncrement, aCallback);
591 return target->Dispatch(asyncInit, nsIEventTarget::DISPATCH_NORMAL);
594 NS_IMETHODIMP
595 Service::OpenDatabase(nsIFile* aDatabaseFile,
596 mozIStorageConnection** _connection) {
597 NS_ENSURE_ARG(aDatabaseFile);
599 // Always ensure that SQLITE_OPEN_CREATE is passed in for compatibility
600 // reasons.
601 int flags =
602 SQLITE_OPEN_READWRITE | SQLITE_OPEN_SHAREDCACHE | SQLITE_OPEN_CREATE;
603 RefPtr<Connection> msc = new Connection(this, flags, Connection::SYNCHRONOUS);
605 nsresult rv = msc->initialize(aDatabaseFile);
606 NS_ENSURE_SUCCESS(rv, rv);
608 msc.forget(_connection);
609 return NS_OK;
612 NS_IMETHODIMP
613 Service::OpenUnsharedDatabase(nsIFile* aDatabaseFile,
614 mozIStorageConnection** _connection) {
615 NS_ENSURE_ARG(aDatabaseFile);
617 // Always ensure that SQLITE_OPEN_CREATE is passed in for compatibility
618 // reasons.
619 int flags =
620 SQLITE_OPEN_READWRITE | SQLITE_OPEN_PRIVATECACHE | SQLITE_OPEN_CREATE;
621 RefPtr<Connection> msc = new Connection(this, flags, Connection::SYNCHRONOUS);
623 nsresult rv = msc->initialize(aDatabaseFile);
624 NS_ENSURE_SUCCESS(rv, rv);
626 msc.forget(_connection);
627 return NS_OK;
630 NS_IMETHODIMP
631 Service::OpenDatabaseWithFileURL(nsIFileURL* aFileURL,
632 const nsACString& aTelemetryFilename,
633 mozIStorageConnection** _connection) {
634 NS_ENSURE_ARG(aFileURL);
636 // Always ensure that SQLITE_OPEN_CREATE is passed in for compatibility
637 // reasons.
638 int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_SHAREDCACHE |
639 SQLITE_OPEN_CREATE | SQLITE_OPEN_URI;
640 RefPtr<Connection> msc = new Connection(this, flags, Connection::SYNCHRONOUS);
642 nsresult rv = msc->initialize(aFileURL, aTelemetryFilename);
643 NS_ENSURE_SUCCESS(rv, rv);
645 msc.forget(_connection);
646 return NS_OK;
649 NS_IMETHODIMP
650 Service::BackupDatabaseFile(nsIFile* aDBFile, const nsAString& aBackupFileName,
651 nsIFile* aBackupParentDirectory, nsIFile** backup) {
652 nsresult rv;
653 nsCOMPtr<nsIFile> parentDir = aBackupParentDirectory;
654 if (!parentDir) {
655 // This argument is optional, and defaults to the same parent directory
656 // as the current file.
657 rv = aDBFile->GetParent(getter_AddRefs(parentDir));
658 NS_ENSURE_SUCCESS(rv, rv);
661 nsCOMPtr<nsIFile> backupDB;
662 rv = parentDir->Clone(getter_AddRefs(backupDB));
663 NS_ENSURE_SUCCESS(rv, rv);
665 rv = backupDB->Append(aBackupFileName);
666 NS_ENSURE_SUCCESS(rv, rv);
668 rv = backupDB->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
669 NS_ENSURE_SUCCESS(rv, rv);
671 nsAutoString fileName;
672 rv = backupDB->GetLeafName(fileName);
673 NS_ENSURE_SUCCESS(rv, rv);
675 rv = backupDB->Remove(false);
676 NS_ENSURE_SUCCESS(rv, rv);
678 backupDB.forget(backup);
680 return aDBFile->CopyTo(parentDir, fileName);
683 ////////////////////////////////////////////////////////////////////////////////
684 //// nsIObserver
686 NS_IMETHODIMP
687 Service::Observe(nsISupports*, const char* aTopic, const char16_t*) {
688 if (strcmp(aTopic, "memory-pressure") == 0) {
689 minimizeMemory();
690 } else if (strcmp(aTopic, "xpcom-shutdown-threads") == 0) {
691 // The Service is kept alive by our strong observer references and
692 // references held by Connection instances. Since we're about to remove the
693 // former and then wait for the latter ones to go away, it behooves us to
694 // hold a strong reference to ourselves so our calls to getConnections() do
695 // not happen on a deleted object.
696 RefPtr<Service> kungFuDeathGrip = this;
698 nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
700 for (size_t i = 0; i < ArrayLength(sObserverTopics); ++i) {
701 (void)os->RemoveObserver(this, sObserverTopics[i]);
704 SpinEventLoopUntil([&]() -> bool {
705 // We must wait until all the closing connections are closed.
706 nsTArray<RefPtr<Connection>> connections;
707 getConnections(connections);
708 for (auto& conn : connections) {
709 if (conn->isClosing()) {
710 return false;
713 return true;
716 #ifdef DEBUG
717 nsTArray<RefPtr<Connection>> connections;
718 getConnections(connections);
719 for (uint32_t i = 0, n = connections.Length(); i < n; i++) {
720 if (!connections[i]->isClosed()) {
721 // getFilename is only the leaf name for the database file,
722 // so it shouldn't contain privacy-sensitive information.
723 CrashReporter::AnnotateCrashReport(
724 CrashReporter::Annotation::StorageConnectionNotClosed,
725 connections[i]->getFilename());
726 printf_stderr("Storage connection not closed: %s",
727 connections[i]->getFilename().get());
728 MOZ_CRASH();
731 #endif
734 return NS_OK;
737 } // namespace storage
738 } // namespace mozilla