tor-browser

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

CacheObserver.h (3703B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #ifndef CacheObserver__h__
      6 #define CacheObserver__h__
      7 
      8 #include "nsIObserver.h"
      9 #include "nsIFile.h"
     10 #include "nsCOMPtr.h"
     11 #include "nsWeakReference.h"
     12 #include "mozilla/StaticPrefs_browser.h"
     13 #include "mozilla/StaticPrefs_privacy.h"
     14 #include "mozilla/StaticPtr.h"
     15 
     16 namespace mozilla {
     17 namespace net {
     18 
     19 class CacheObserver : public nsIObserver, public nsSupportsWeakReference {
     20  virtual ~CacheObserver() = default;
     21 
     22  NS_DECL_THREADSAFE_ISUPPORTS
     23  NS_DECL_NSIOBSERVER
     24 
     25  static nsresult Init();
     26  static nsresult Shutdown();
     27  static CacheObserver* Self() { return sSelf; }
     28 
     29  // Access to preferences
     30  static bool UseDiskCache() {
     31    return StaticPrefs::browser_cache_disk_enable();
     32  }
     33  static bool UseMemoryCache() {
     34    return StaticPrefs::browser_cache_memory_enable();
     35  }
     36  static uint32_t MetadataMemoryLimit()  // result in kilobytes.
     37  {
     38    return StaticPrefs::browser_cache_disk_metadata_memory_limit();
     39  }
     40  static uint32_t MemoryCacheCapacity();            // result in kilobytes.
     41  static uint32_t DiskCacheCapacity();              // result in kilobytes.
     42  static void SetSmartDiskCacheCapacity(uint32_t);  // parameter in kilobytes.
     43  static uint32_t DiskFreeSpaceSoftLimit()          // result in kilobytes.
     44  {
     45    return StaticPrefs::browser_cache_disk_free_space_soft_limit();
     46  }
     47  static uint32_t DiskFreeSpaceHardLimit()  // result in kilobytes.
     48  {
     49    return StaticPrefs::browser_cache_disk_free_space_hard_limit();
     50  }
     51  static bool SmartCacheSizeEnabled() {
     52    return StaticPrefs::browser_cache_disk_smart_size_enabled();
     53  }
     54  static uint32_t PreloadChunkCount() {
     55    return StaticPrefs::browser_cache_disk_preload_chunk_count();
     56  }
     57  static uint32_t MaxMemoryEntrySize()  // result in kilobytes.
     58  {
     59    return StaticPrefs::browser_cache_memory_max_entry_size();
     60  }
     61  static uint32_t MaxDiskEntrySize()  // result in kilobytes.
     62  {
     63    return StaticPrefs::browser_cache_disk_max_entry_size();
     64  }
     65  static uint32_t MaxDiskChunksMemoryUsage(
     66      bool aPriority)  // result in kilobytes.
     67  {
     68    return aPriority
     69               ? StaticPrefs::
     70                     browser_cache_disk_max_priority_chunks_memory_usage()
     71               : StaticPrefs::browser_cache_disk_max_chunks_memory_usage();
     72  }
     73  static uint32_t HalfLifeSeconds() { return sHalfLifeHours * 60.0F * 60.0F; }
     74  static bool ClearCacheOnShutdown() {
     75    if (!StaticPrefs::privacy_sanitize_sanitizeOnShutdown()) {
     76      return false;
     77    }
     78    if (StaticPrefs::privacy_sanitize_useOldClearHistoryDialog()) {
     79      return StaticPrefs::privacy_clearOnShutdown_cache();
     80    }
     81    // use the new cache clearing pref for the new clear history dialog
     82    return StaticPrefs::privacy_clearOnShutdown_v2_cache();
     83  }
     84  static void ParentDirOverride(nsIFile** aDir);
     85 
     86  static bool EntryIsTooBig(int64_t aSize, bool aUsingDisk);
     87 
     88  static uint32_t MaxShutdownIOLag() {
     89    return StaticPrefs::browser_cache_max_shutdown_io_lag();
     90  }
     91  static bool IsPastShutdownIOLag();
     92 
     93  static bool ShuttingDown() {
     94    return sShutdownDemandedTime != PR_INTERVAL_NO_TIMEOUT;
     95  }
     96 
     97 private:
     98  static StaticRefPtr<CacheObserver> sSelf;
     99 
    100  void AttachToPreferences();
    101 
    102  static int32_t sAutoMemoryCacheCapacity;
    103  static Atomic<uint32_t, Relaxed> sSmartDiskCacheCapacity;
    104  static float sHalfLifeHours;
    105  static Atomic<PRIntervalTime> sShutdownDemandedTime;
    106 
    107  // Non static properties, accessible via sSelf
    108  nsCOMPtr<nsIFile> mCacheParentDirectoryOverride;
    109 };
    110 
    111 }  // namespace net
    112 }  // namespace mozilla
    113 
    114 #endif