tor-browser

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

FileInfoImpl.h (4014B)


      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 #ifndef DOM_INDEXEDDB_FILEINFOIMPL_H_
      8 #define DOM_INDEXEDDB_FILEINFOIMPL_H_
      9 
     10 #include "FileInfo.h"
     11 #include "mozilla/Mutex.h"
     12 #include "mozilla/dom/QMResult.h"
     13 #include "mozilla/dom/quota/QuotaCommon.h"
     14 #include "mozilla/dom/quota/ResultExtensions.h"
     15 #include "nsIFile.h"
     16 
     17 namespace mozilla::dom::indexedDB {
     18 
     19 template <typename FileManager>
     20 FileInfo<FileManager>::FileInfo(
     21    const typename FileManager::FileInfoManagerGuard& aGuard,
     22    SafeRefPtr<FileManager> aFileManager, const int64_t aFileId,
     23    const nsrefcnt aInitialDBRefCnt)
     24    : FileInfoBase{aFileId},
     25      mDBRefCnt(aInitialDBRefCnt),
     26      mFileManager(std::move(aFileManager)) {
     27  MOZ_ASSERT(mFileManager);
     28 }
     29 
     30 template <typename FileManager>
     31 void FileInfo<FileManager>::AddRef() {
     32  AutoLockType lock(FileManager::Mutex());
     33 
     34  LockedAddRef();
     35 }
     36 
     37 template <typename FileManager>
     38 void FileInfo<FileManager>::Release(const bool aSyncDeleteFile) {
     39  UpdateReferences(mRefCnt, -1, aSyncDeleteFile);
     40 }
     41 
     42 template <typename FileManager>
     43 void FileInfo<FileManager>::UpdateDBRefs(int32_t aDelta) {
     44  UpdateReferences(mDBRefCnt, aDelta);
     45 }
     46 
     47 template <typename FileManager>
     48 void FileInfo<FileManager>::GetReferences(int32_t* const aRefCnt,
     49                                          int32_t* const aDBRefCnt) {
     50  AutoLockType lock(FileManager::Mutex());
     51 
     52  if (aRefCnt) {
     53    *aRefCnt = mRefCnt;
     54  }
     55 
     56  if (aDBRefCnt) {
     57    *aDBRefCnt = mDBRefCnt;
     58  }
     59 }
     60 
     61 template <typename FileManager>
     62 FileManager& FileInfo<FileManager>::Manager() const {
     63  return *mFileManager;
     64 }
     65 
     66 template <typename FileManager>
     67 void FileInfo<FileManager>::UpdateReferences(ThreadSafeAutoRefCnt& aRefCount,
     68                                             const int32_t aDelta,
     69                                             const bool aSyncDeleteFile) {
     70  bool needsCleanup;
     71  {
     72    AutoLockType lock(FileManager::Mutex());
     73 
     74    aRefCount = aRefCount + aDelta;
     75 
     76    if (mRefCnt + mDBRefCnt > 0) {
     77      return;
     78    }
     79 
     80    mFileManager->RemoveFileInfo(Id(), lock);
     81 
     82    // If the FileManager was already invalidated, we don't need to do any
     83    // cleanup anymore. In that case, the entire origin directory has already
     84    // been deleted by the quota manager, and we don't need to delete individual
     85    // files.
     86    needsCleanup = !mFileManager->Invalidated();
     87  }
     88 
     89  if (needsCleanup) {
     90    if (aSyncDeleteFile) {
     91      QM_WARNONLY_TRY(QM_TO_RESULT(mFileManager->SyncDeleteFile(Id())));
     92    } else {
     93      Cleanup();
     94    }
     95  }
     96 
     97  delete this;
     98 }
     99 
    100 template <typename FileManager>
    101 void FileInfo<FileManager>::LockedAddRef() {
    102  FileManager::Mutex().AssertCurrentThreadOwns();
    103 
    104  ++mRefCnt;
    105 }
    106 
    107 template <typename FileManager>
    108 bool FileInfo<FileManager>::LockedClearDBRefs(
    109    const typename FileManager::FileInfoManagerGuard&) {
    110  FileManager::Mutex().AssertCurrentThreadOwns();
    111 
    112  mDBRefCnt = 0;
    113 
    114  if (mRefCnt) {
    115    return true;
    116  }
    117 
    118  // In this case, we are not responsible for removing the FileInfo from the
    119  // hashtable. It's up to FileManager which is the only caller of this method.
    120 
    121  MOZ_ASSERT(mFileManager->Invalidated());
    122 
    123  delete this;
    124 
    125  return false;
    126 }
    127 
    128 template <typename FileManager>
    129 void FileInfo<FileManager>::Cleanup() {
    130  QM_WARNONLY_TRY(QM_TO_RESULT(mFileManager->AsyncDeleteFile(Id())));
    131 }
    132 
    133 template <typename FileManager>
    134 nsCOMPtr<nsIFile> FileInfo<FileManager>::GetFileForFileInfo() const {
    135  const nsCOMPtr<nsIFile> directory = Manager().GetDirectory();
    136  if (NS_WARN_IF(!directory)) {
    137    return nullptr;
    138  }
    139 
    140  nsCOMPtr<nsIFile> file = FileManager::GetFileForId(directory, Id());
    141  if (NS_WARN_IF(!file)) {
    142    return nullptr;
    143  }
    144 
    145  return file;
    146 }
    147 
    148 }  // namespace mozilla::dom::indexedDB
    149 
    150 #endif  // DOM_INDEXEDDB_FILEINFOIMPL_H_