tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

StorageHelpers.cpp (2015B)


      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/. */
      6 
      7 #include "mozilla/dom/quota/StorageHelpers.h"
      8 
      9 #include "mozilla/dom/quota/QuotaCommon.h"
     10 #include "mozilla/dom/quota/ResultExtensions.h"
     11 
     12 namespace mozilla::dom::quota {
     13 
     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) {}
     21 
     22 AutoDatabaseAttacher::~AutoDatabaseAttacher() {
     23  if (mAttached) {
     24    QM_WARNONLY_TRY(MOZ_TO_RESULT(Detach()));
     25  }
     26 }
     27 
     28 nsresult AutoDatabaseAttacher::Attach() {
     29  MOZ_ASSERT(mConnection);
     30  MOZ_ASSERT(mDatabaseFile);
     31  MOZ_ASSERT(!mAttached);
     32 
     33 #ifdef DEBUG
     34  {
     35    QM_TRY_INSPECT(const bool& exists,
     36                   MOZ_TO_RESULT_INVOKE_MEMBER(mDatabaseFile, Exists));
     37 
     38    MOZ_ASSERT(exists);
     39  }
     40 #endif
     41 
     42  QM_TRY_INSPECT(const auto& path, MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
     43                                       nsString, mDatabaseFile, GetPath));
     44 
     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));
     50 
     51  QM_TRY(MOZ_TO_RESULT(stmt->BindStringByName("path"_ns, path)));
     52  QM_TRY(MOZ_TO_RESULT(stmt->Execute()));
     53 
     54  mAttached = true;
     55 
     56  return NS_OK;
     57 }
     58 
     59 nsresult AutoDatabaseAttacher::Detach() {
     60  MOZ_ASSERT(mConnection);
     61  MOZ_ASSERT(mAttached);
     62 
     63  QM_TRY(MOZ_TO_RESULT(
     64      mConnection->ExecuteSimpleSQL("DETACH DATABASE "_ns + mSchemaName)));
     65 
     66  mAttached = false;
     67  return NS_OK;
     68 }
     69 
     70 }  // namespace mozilla::dom::quota