tor-browser

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

FileSystemDatabaseManager.cpp (2751B)


      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
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "FileSystemDatabaseManager.h"
      8 
      9 #include "FileSystemDatabaseManagerVersion001.h"
     10 #include "FileSystemDatabaseManagerVersion002.h"
     11 #include "FileSystemFileManager.h"
     12 #include "ResultConnection.h"
     13 #include "ResultStatement.h"
     14 #include "SchemaVersion001.h"
     15 #include "SchemaVersion002.h"
     16 #include "mozilla/dom/quota/QuotaCommon.h"
     17 #include "mozilla/dom/quota/ResultExtensions.h"
     18 #include "nsCOMPtr.h"
     19 #include "nsIFile.h"
     20 
     21 namespace mozilla::dom::fs::data {
     22 
     23 namespace {
     24 
     25 Result<Usage, QMResult> GetFileUsage(const ResultConnection& aConnection) {
     26  DatabaseVersion version = 0;
     27  QM_TRY(QM_TO_RESULT(aConnection->GetSchemaVersion(&version)));
     28 
     29  switch (version) {
     30    case 0: {
     31      return 0;
     32    }
     33 
     34    case 1: {
     35      QM_TRY_RETURN(
     36          FileSystemDatabaseManagerVersion001::GetFileUsage(aConnection));
     37    }
     38 
     39    case 2: {
     40      QM_TRY_RETURN(
     41          FileSystemDatabaseManagerVersion002::GetFileUsage(aConnection));
     42    }
     43 
     44    default:
     45      break;
     46  }
     47 
     48  return Err(QMResult(NS_ERROR_NOT_IMPLEMENTED));
     49 }
     50 
     51 }  // namespace
     52 
     53 /* static */
     54 nsresult FileSystemDatabaseManager::RescanUsages(
     55    const ResultConnection& aConnection,
     56    const quota::OriginMetadata& aOriginMetadata) {
     57  DatabaseVersion version = 0;
     58  QM_TRY(MOZ_TO_RESULT(aConnection->GetSchemaVersion(&version)));
     59 
     60  switch (version) {
     61    case 0: {
     62      return NS_OK;
     63    }
     64 
     65    case 1:
     66      return FileSystemDatabaseManagerVersion001::RescanTrackedUsages(
     67          aConnection, aOriginMetadata);
     68 
     69    case 2: {
     70      return FileSystemDatabaseManagerVersion002::RescanTrackedUsages(
     71          aConnection, aOriginMetadata);
     72    }
     73 
     74    default:
     75      break;
     76  }
     77 
     78  return NS_ERROR_NOT_IMPLEMENTED;
     79 }
     80 
     81 /* static */
     82 Result<quota::UsageInfo, QMResult> FileSystemDatabaseManager::GetUsage(
     83    const ResultConnection& aConnection,
     84    const quota::OriginMetadata& aOriginMetadata) {
     85  QM_TRY_INSPECT(const auto& databaseFile, GetDatabaseFile(aOriginMetadata));
     86 
     87  // If database is deleted between connection creation and now, error
     88  int64_t dbSize = 0;
     89  QM_TRY(QM_TO_RESULT(databaseFile->GetFileSize(&dbSize)));
     90 
     91  quota::UsageInfo result(quota::DatabaseUsageType(Some(dbSize)));
     92 
     93  QM_TRY_INSPECT(const Usage& fileUsage, GetFileUsage(aConnection));
     94 
     95  // XXX: DatabaseUsage is currently total usage for most forms of storage
     96  result += quota::DatabaseUsageType(Some(fileUsage));
     97 
     98  return result;
     99 }
    100 
    101 }  // namespace mozilla::dom::fs::data