tor-browser

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

QuotaClientImpl.h (5375B)


      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 mozilla_dom_cache_QuotaClientImpl_h
      8 #define mozilla_dom_cache_QuotaClientImpl_h
      9 
     10 #include "CacheCipherKeyManager.h"
     11 #include "mozilla/RefPtr.h"
     12 #include "mozilla/dom/QMResult.h"
     13 #include "mozilla/dom/cache/FileUtils.h"
     14 #include "mozilla/dom/cache/QuotaClient.h"
     15 #include "mozilla/dom/cache/Types.h"
     16 #include "mozilla/dom/quota/ResultExtensions.h"
     17 
     18 namespace mozilla::dom::cache {
     19 
     20 class CacheQuotaClient final : public quota::Client {
     21  static CacheQuotaClient* sInstance;
     22 
     23 public:
     24  using OriginMetadata = quota::OriginMetadata;
     25  using PersistenceType = quota::PersistenceType;
     26  using UsageInfo = quota::UsageInfo;
     27 
     28  CacheQuotaClient();
     29 
     30  static CacheQuotaClient* Get();
     31 
     32  virtual Type GetType() override;
     33 
     34  virtual Result<UsageInfo, nsresult> InitOrigin(
     35      PersistenceType aPersistenceType, const OriginMetadata& aOriginMetadata,
     36      const AtomicBool& aCanceled) override;
     37 
     38  virtual nsresult InitOriginWithoutTracking(
     39      PersistenceType aPersistenceType, const OriginMetadata& aOriginMetadata,
     40      const AtomicBool& aCanceled) override;
     41 
     42  virtual Result<UsageInfo, nsresult> GetUsageForOrigin(
     43      PersistenceType aPersistenceType, const OriginMetadata& aOriginMetadata,
     44      const AtomicBool& aCanceled) override;
     45 
     46  virtual void OnOriginClearCompleted(
     47      const OriginMetadata& aOriginMetadata) override;
     48 
     49  void OnRepositoryClearCompleted(PersistenceType aPersistenceType) override;
     50 
     51  virtual void ReleaseIOThreadObjects() override;
     52 
     53  void AbortOperationsForLocks(
     54      const DirectoryLockIdTable& aDirectoryLockIds) override;
     55 
     56  virtual void AbortOperationsForProcess(
     57      ContentParentId aContentParentId) override;
     58 
     59  virtual void AbortAllOperations() override;
     60 
     61  virtual void StartIdleMaintenance() override;
     62 
     63  virtual void StopIdleMaintenance() override;
     64 
     65  nsresult UpgradeStorageFrom2_0To2_1(nsIFile* aDirectory) override;
     66 
     67  template <typename Callable>
     68  nsresult MaybeUpdatePaddingFileInternal(nsIFile& aBaseDir,
     69                                          mozIStorageConnection& aConn,
     70                                          const int64_t aIncreaseSize,
     71                                          const int64_t aDecreaseSize,
     72                                          Callable&& aCommitHook) {
     73    MOZ_ASSERT(!NS_IsMainThread());
     74    MOZ_DIAGNOSTIC_ASSERT(aIncreaseSize >= 0);
     75    MOZ_DIAGNOSTIC_ASSERT(aDecreaseSize >= 0);
     76 
     77    // Temporary should be removed at the end of each action. If not, it means
     78    // the failure happened.
     79    const bool temporaryPaddingFileExist =
     80        DirectoryPaddingFileExists(aBaseDir, DirPaddingFile::TMP_FILE);
     81 
     82    if (aIncreaseSize == aDecreaseSize && !temporaryPaddingFileExist) {
     83      // Early return here, since most cache actions won't modify padding size.
     84      QM_TRY(MOZ_TO_RESULT(aCommitHook()));
     85 
     86      return NS_OK;
     87    }
     88 
     89    // Don't delete the temporary padding file in case of an error to force the
     90    // next action recalculate the padding size.
     91    QM_TRY(MOZ_TO_RESULT(
     92        UpdateDirectoryPaddingFile(aBaseDir, aConn, aIncreaseSize,
     93                                   aDecreaseSize, temporaryPaddingFileExist)));
     94 
     95    // Don't delete the temporary padding file in case of an error to force the
     96    // next action recalculate the padding size.
     97    QM_TRY(MOZ_TO_RESULT(aCommitHook()));
     98 
     99    QM_WARNONLY_TRY(MOZ_TO_RESULT(DirectoryPaddingFinalizeWrite(aBaseDir)),
    100                    ([&aBaseDir](const nsresult) {
    101                      // Force restore file next time.
    102                      QM_WARNONLY_TRY(QM_TO_RESULT(DirectoryPaddingDeleteFile(
    103                          aBaseDir, DirPaddingFile::FILE)));
    104 
    105                      // Ensure that we are able to force the padding file to
    106                      // be restored.
    107                      MOZ_ASSERT(DirectoryPaddingFileExists(
    108                          aBaseDir, DirPaddingFile::TMP_FILE));
    109 
    110                      // Since both the body file and header have been stored
    111                      // in the file-system, just make the action be resolve
    112                      // and let the padding file be restored in the next
    113                      // action.
    114                    }));
    115 
    116    return NS_OK;
    117  }
    118 
    119  nsresult RestorePaddingFileInternal(nsIFile* aBaseDir,
    120                                      mozIStorageConnection* aConn);
    121 
    122  nsresult WipePaddingFileInternal(
    123      const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aBaseDir);
    124 
    125  RefPtr<CipherKeyManager> GetOrCreateCipherKeyManager(
    126      const quota::PrincipalMetadata& aMetadata);
    127 
    128 private:
    129  ~CacheQuotaClient();
    130 
    131  void InitiateShutdown() override;
    132  bool IsShutdownCompleted() const override;
    133  nsCString GetShutdownStatus() const override;
    134  void ForceKillActors() override;
    135  void FinalizeShutdown() override;
    136 
    137  // Should always be accessed from QM IO thread.
    138  nsTHashMap<nsCStringHashKey, RefPtr<CipherKeyManager>> mCipherKeyManagers;
    139 
    140  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CacheQuotaClient, override)
    141 };
    142 
    143 }  // namespace mozilla::dom::cache
    144 
    145 #endif  // mozilla_dom_cache_QuotaClientImpl_h