Bug 1874684 - Part 6: Limit day length calculations to safe integers. r=mgaudet
[gecko.git] / dom / quota / StorageHelpers.cpp
blob84cd80629795b4b3d0b08ba0c6044c740bcb84ba
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/quota/StorageHelpers.h"
9 #include "mozilla/dom/quota/QuotaCommon.h"
10 #include "mozilla/dom/quota/ResultExtensions.h"
12 namespace mozilla::dom::quota {
14 AutoDatabaseAttacher::AutoDatabaseAttacher(
15 nsCOMPtr<mozIStorageConnection> aConnection,
16 nsCOMPtr<nsIFile> aDatabaseFile, const nsLiteralCString& aSchemaName)
17 : mConnection(std::move(aConnection)),
18 mDatabaseFile(std::move(aDatabaseFile)),
19 mSchemaName(aSchemaName),
20 mAttached(false) {}
22 AutoDatabaseAttacher::~AutoDatabaseAttacher() {
23 if (mAttached) {
24 QM_WARNONLY_TRY(MOZ_TO_RESULT(Detach()));
28 nsresult AutoDatabaseAttacher::Attach() {
29 MOZ_ASSERT(mConnection);
30 MOZ_ASSERT(mDatabaseFile);
31 MOZ_ASSERT(!mAttached);
33 #ifdef DEBUG
35 QM_TRY_INSPECT(const bool& exists,
36 MOZ_TO_RESULT_INVOKE_MEMBER(mDatabaseFile, Exists));
38 MOZ_ASSERT(exists);
40 #endif
42 QM_TRY_INSPECT(const auto& path, MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
43 nsString, mDatabaseFile, GetPath));
45 QM_TRY_INSPECT(
46 const auto& stmt,
47 MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
48 nsCOMPtr<mozIStorageStatement>, mConnection, CreateStatement,
49 "ATTACH DATABASE :path AS "_ns + mSchemaName + ";"_ns));
51 QM_TRY(MOZ_TO_RESULT(stmt->BindStringByName("path"_ns, path)));
52 QM_TRY(MOZ_TO_RESULT(stmt->Execute()));
54 mAttached = true;
56 return NS_OK;
59 nsresult AutoDatabaseAttacher::Detach() {
60 MOZ_ASSERT(mConnection);
61 MOZ_ASSERT(mAttached);
63 QM_TRY(MOZ_TO_RESULT(
64 mConnection->ExecuteSimpleSQL("DETACH DATABASE "_ns + mSchemaName)));
66 mAttached = false;
67 return NS_OK;
70 } // namespace mozilla::dom::quota