2013-09-20 02:11:25 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "CacheObserver.h"
|
|
|
|
|
|
|
|
#include "CacheStorageService.h"
|
|
|
|
#include "CacheFileIOManager.h"
|
2013-11-20 14:20:16 -08:00
|
|
|
#include "LoadContextInfo.h"
|
|
|
|
#include "nsICacheStorage.h"
|
2013-09-20 02:11:25 -07:00
|
|
|
#include "nsIObserverService.h"
|
2013-11-20 14:20:16 -08:00
|
|
|
#include "mozIApplicationClearPrivateDataParams.h"
|
2013-09-20 02:11:25 -07:00
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2014-04-06 11:45:18 -07:00
|
|
|
#include "prsystem.h"
|
2014-01-08 15:27:33 -08:00
|
|
|
#include <time.h>
|
2014-04-06 11:45:18 -07:00
|
|
|
#include <math.h>
|
2013-09-20 02:11:25 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
CacheObserver* CacheObserver::sSelf = nullptr;
|
|
|
|
|
2014-05-15 16:31:26 -07:00
|
|
|
static uint32_t const kDefaultUseNewCache = 1; // Use the new cache by default
|
2013-09-20 02:11:25 -07:00
|
|
|
uint32_t CacheObserver::sUseNewCache = kDefaultUseNewCache;
|
2014-02-18 10:06:06 -08:00
|
|
|
|
|
|
|
static bool sUseNewCacheTemp = false; // Temp trigger to not lose early adopters
|
|
|
|
|
2014-02-05 11:29:54 -08:00
|
|
|
static int32_t const kAutoDeleteCacheVersion = -1; // Auto-delete off by default
|
|
|
|
static int32_t sAutoDeleteCacheVersion = kAutoDeleteCacheVersion;
|
2013-09-20 02:11:25 -07:00
|
|
|
|
2014-01-08 15:27:33 -08:00
|
|
|
static int32_t const kDefaultHalfLifeExperiment = -1; // Disabled
|
|
|
|
int32_t CacheObserver::sHalfLifeExperiment = kDefaultHalfLifeExperiment;
|
|
|
|
|
|
|
|
static uint32_t const kDefaultHalfLifeHours = 6; // 6 hours
|
|
|
|
uint32_t CacheObserver::sHalfLifeHours = kDefaultHalfLifeHours;
|
|
|
|
|
2014-02-03 16:53:00 -08:00
|
|
|
static bool const kDefaultUseDiskCache = true;
|
|
|
|
bool CacheObserver::sUseDiskCache = kDefaultUseDiskCache;
|
|
|
|
|
|
|
|
static bool const kDefaultUseMemoryCache = true;
|
|
|
|
bool CacheObserver::sUseMemoryCache = kDefaultUseMemoryCache;
|
|
|
|
|
2014-04-06 11:45:18 -07:00
|
|
|
static uint32_t const kDefaultMetadataMemoryLimit = 250; // 0.25 MB
|
|
|
|
uint32_t CacheObserver::sMetadataMemoryLimit = kDefaultMetadataMemoryLimit;
|
|
|
|
|
|
|
|
static int32_t const kDefaultMemoryCacheCapacity = -1; // autodetect
|
|
|
|
int32_t CacheObserver::sMemoryCacheCapacity = kDefaultMemoryCacheCapacity;
|
|
|
|
// Cache of the calculated memory capacity based on the system memory size
|
|
|
|
int32_t CacheObserver::sAutoMemoryCacheCapacity = -1;
|
|
|
|
|
2014-02-03 16:53:00 -08:00
|
|
|
static uint32_t const kDefaultDiskCacheCapacity = 250 * 1024; // 250 MB
|
|
|
|
uint32_t CacheObserver::sDiskCacheCapacity = kDefaultDiskCacheCapacity;
|
|
|
|
|
2014-04-22 02:21:00 -07:00
|
|
|
static bool const kDefaultSmartCacheSizeEnabled = false;
|
|
|
|
bool CacheObserver::sSmartCacheSizeEnabled = kDefaultSmartCacheSizeEnabled;
|
|
|
|
|
2014-05-02 11:15:14 -07:00
|
|
|
static uint32_t const kDefaultPreloadChunkCount = 4;
|
|
|
|
uint32_t CacheObserver::sPreloadChunkCount = kDefaultPreloadChunkCount;
|
|
|
|
|
2014-02-03 16:53:00 -08:00
|
|
|
static uint32_t const kDefaultMaxMemoryEntrySize = 4 * 1024; // 4 MB
|
|
|
|
uint32_t CacheObserver::sMaxMemoryEntrySize = kDefaultMaxMemoryEntrySize;
|
|
|
|
|
|
|
|
static uint32_t const kDefaultMaxDiskEntrySize = 50 * 1024; // 50 MB
|
|
|
|
uint32_t CacheObserver::sMaxDiskEntrySize = kDefaultMaxDiskEntrySize;
|
|
|
|
|
|
|
|
static uint32_t const kDefaultCompressionLevel = 1;
|
|
|
|
uint32_t CacheObserver::sCompressionLevel = kDefaultCompressionLevel;
|
|
|
|
|
2014-04-09 01:06:59 -07:00
|
|
|
static bool kDefaultSanitizeOnShutdown = false;
|
|
|
|
bool CacheObserver::sSanitizeOnShutdown = kDefaultSanitizeOnShutdown;
|
|
|
|
|
|
|
|
static bool kDefaultClearCacheOnShutdown = false;
|
|
|
|
bool CacheObserver::sClearCacheOnShutdown = kDefaultClearCacheOnShutdown;
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(CacheObserver,
|
|
|
|
nsIObserver,
|
|
|
|
nsISupportsWeakReference)
|
2013-09-20 02:11:25 -07:00
|
|
|
|
2014-02-26 15:11:38 -08:00
|
|
|
// static
|
2013-09-20 02:11:25 -07:00
|
|
|
nsresult
|
|
|
|
CacheObserver::Init()
|
|
|
|
{
|
|
|
|
if (sSelf) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (!obs) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
sSelf = new CacheObserver();
|
|
|
|
NS_ADDREF(sSelf);
|
|
|
|
|
|
|
|
obs->AddObserver(sSelf, "prefservice:after-app-defaults", true);
|
|
|
|
obs->AddObserver(sSelf, "profile-do-change", true);
|
2014-02-05 11:29:54 -08:00
|
|
|
obs->AddObserver(sSelf, "sessionstore-windows-restored", true);
|
2013-09-20 02:11:25 -07:00
|
|
|
obs->AddObserver(sSelf, "profile-before-change", true);
|
|
|
|
obs->AddObserver(sSelf, "xpcom-shutdown", true);
|
|
|
|
obs->AddObserver(sSelf, "last-pb-context-exited", true);
|
2013-11-20 14:20:16 -08:00
|
|
|
obs->AddObserver(sSelf, "webapps-clear-data", true);
|
2013-09-20 02:11:25 -07:00
|
|
|
obs->AddObserver(sSelf, "memory-pressure", true);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-26 15:11:38 -08:00
|
|
|
// static
|
2013-09-20 02:11:25 -07:00
|
|
|
nsresult
|
|
|
|
CacheObserver::Shutdown()
|
|
|
|
{
|
|
|
|
if (!sSelf) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_RELEASE(sSelf);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CacheObserver::AttachToPreferences()
|
|
|
|
{
|
2014-02-05 11:29:54 -08:00
|
|
|
sAutoDeleteCacheVersion = mozilla::Preferences::GetInt(
|
|
|
|
"browser.cache.auto_delete_cache_version", kAutoDeleteCacheVersion);
|
|
|
|
|
2014-02-03 16:53:00 -08:00
|
|
|
mozilla::Preferences::AddUintVarCache(
|
|
|
|
&sUseNewCache, "browser.cache.use_new_backend", kDefaultUseNewCache);
|
2014-02-18 10:06:06 -08:00
|
|
|
mozilla::Preferences::AddBoolVarCache(
|
|
|
|
&sUseNewCacheTemp, "browser.cache.use_new_backend_temp", false);
|
2014-02-03 16:53:00 -08:00
|
|
|
|
|
|
|
mozilla::Preferences::AddBoolVarCache(
|
|
|
|
&sUseDiskCache, "browser.cache.disk.enable", kDefaultUseDiskCache);
|
|
|
|
mozilla::Preferences::AddBoolVarCache(
|
|
|
|
&sUseMemoryCache, "browser.cache.memory.enable", kDefaultUseMemoryCache);
|
|
|
|
|
2013-09-20 02:11:25 -07:00
|
|
|
mozilla::Preferences::AddUintVarCache(
|
2014-04-06 11:45:18 -07:00
|
|
|
&sMetadataMemoryLimit, "browser.cache.disk.metadata_memory_limit", kDefaultMetadataMemoryLimit);
|
2014-02-03 16:53:00 -08:00
|
|
|
|
2013-09-20 02:11:25 -07:00
|
|
|
mozilla::Preferences::AddUintVarCache(
|
2014-02-03 16:53:00 -08:00
|
|
|
&sDiskCacheCapacity, "browser.cache.disk.capacity", kDefaultDiskCacheCapacity);
|
2014-04-22 02:21:00 -07:00
|
|
|
mozilla::Preferences::AddBoolVarCache(
|
|
|
|
&sSmartCacheSizeEnabled, "browser.cache.disk.smart_size.enabled", kDefaultSmartCacheSizeEnabled);
|
2014-04-06 11:45:18 -07:00
|
|
|
mozilla::Preferences::AddIntVarCache(
|
|
|
|
&sMemoryCacheCapacity, "browser.cache.memory.capacity", kDefaultMemoryCacheCapacity);
|
2014-02-03 16:53:00 -08:00
|
|
|
|
2014-05-02 11:15:14 -07:00
|
|
|
mozilla::Preferences::AddUintVarCache(
|
|
|
|
&sPreloadChunkCount, "browser.cache.disk.preload_chunk_count", kDefaultPreloadChunkCount);
|
|
|
|
|
2014-02-03 16:53:00 -08:00
|
|
|
mozilla::Preferences::AddUintVarCache(
|
|
|
|
&sMaxDiskEntrySize, "browser.cache.disk.max_entry_size", kDefaultMaxDiskEntrySize);
|
2014-04-06 11:45:18 -07:00
|
|
|
mozilla::Preferences::AddUintVarCache(
|
|
|
|
&sMaxMemoryEntrySize, "browser.cache.memory.max_entry_size", kDefaultMaxMemoryEntrySize);
|
2014-02-03 16:53:00 -08:00
|
|
|
|
|
|
|
// http://mxr.mozilla.org/mozilla-central/source/netwerk/cache/nsCacheEntryDescriptor.cpp#367
|
|
|
|
mozilla::Preferences::AddUintVarCache(
|
|
|
|
&sCompressionLevel, "browser.cache.compression_level", kDefaultCompressionLevel);
|
2014-01-08 15:27:33 -08:00
|
|
|
|
2014-02-26 15:11:38 -08:00
|
|
|
mozilla::Preferences::GetComplex(
|
|
|
|
"browser.cache.disk.parent_directory", NS_GET_IID(nsIFile),
|
|
|
|
getter_AddRefs(mCacheParentDirectoryOverride));
|
|
|
|
|
2014-04-04 05:42:06 -07:00
|
|
|
// First check the default value. If it is at -1, the experient
|
|
|
|
// is turned off. If it is at 0, then use the user pref value
|
|
|
|
// instead.
|
|
|
|
sHalfLifeExperiment = mozilla::Preferences::GetDefaultInt(
|
2014-01-08 15:27:33 -08:00
|
|
|
"browser.cache.frecency_experiment", kDefaultHalfLifeExperiment);
|
|
|
|
|
|
|
|
if (sHalfLifeExperiment == 0) {
|
2014-04-04 05:42:06 -07:00
|
|
|
// Default preferences indicate we want to run the experiment,
|
|
|
|
// hence read the user value.
|
|
|
|
sHalfLifeExperiment = mozilla::Preferences::GetInt(
|
|
|
|
"browser.cache.frecency_experiment", sHalfLifeExperiment);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sHalfLifeExperiment == 0) {
|
|
|
|
// The experiment has not yet been initialized but is engaged, do
|
|
|
|
// the initialization now.
|
2014-01-08 15:27:33 -08:00
|
|
|
srand(time(NULL));
|
|
|
|
sHalfLifeExperiment = (rand() % 4) + 1;
|
2014-04-04 05:42:06 -07:00
|
|
|
// Store the experiemnt value, since we need it not to change between
|
|
|
|
// browser sessions.
|
2014-01-08 15:27:33 -08:00
|
|
|
mozilla::Preferences::SetInt(
|
|
|
|
"browser.cache.frecency_experiment", sHalfLifeExperiment);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (sHalfLifeExperiment) {
|
|
|
|
case 1: // The experiment is engaged
|
|
|
|
sHalfLifeHours = 6;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
sHalfLifeHours = 24;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
sHalfLifeHours = 7 * 24;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
sHalfLifeHours = 50 * 24;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case -1:
|
|
|
|
default: // The experiment is off or broken
|
|
|
|
sHalfLifeExperiment = -1;
|
|
|
|
sHalfLifeHours = std::max(1U, std::min(1440U, mozilla::Preferences::GetUint(
|
|
|
|
"browser.cache.frecency_half_life_hours", kDefaultHalfLifeHours)));
|
|
|
|
break;
|
|
|
|
}
|
2014-04-09 01:06:59 -07:00
|
|
|
|
|
|
|
mozilla::Preferences::AddBoolVarCache(
|
|
|
|
&sSanitizeOnShutdown, "privacy.sanitize.sanitizeOnShutdown", kDefaultSanitizeOnShutdown);
|
|
|
|
mozilla::Preferences::AddBoolVarCache(
|
|
|
|
&sClearCacheOnShutdown, "privacy.clearOnShutdown.cache", kDefaultClearCacheOnShutdown);
|
2013-09-20 02:11:25 -07:00
|
|
|
}
|
|
|
|
|
2014-04-06 11:45:18 -07:00
|
|
|
// static
|
|
|
|
uint32_t const CacheObserver::MemoryCacheCapacity()
|
|
|
|
{
|
|
|
|
if (sMemoryCacheCapacity >= 0)
|
|
|
|
return sMemoryCacheCapacity << 10;
|
|
|
|
|
|
|
|
if (sAutoMemoryCacheCapacity != -1)
|
|
|
|
return sAutoMemoryCacheCapacity;
|
|
|
|
|
|
|
|
static uint64_t bytes = PR_GetPhysicalMemorySize();
|
|
|
|
// If getting the physical memory failed, arbitrarily assume
|
|
|
|
// 32 MB of RAM. We use a low default to have a reasonable
|
|
|
|
// size on all the devices we support.
|
|
|
|
if (bytes == 0)
|
|
|
|
bytes = 32 * 1024 * 1024;
|
|
|
|
|
|
|
|
// Conversion from unsigned int64_t to double doesn't work on all platforms.
|
|
|
|
// We need to truncate the value at INT64_MAX to make sure we don't
|
|
|
|
// overflow.
|
|
|
|
if (bytes > INT64_MAX)
|
|
|
|
bytes = INT64_MAX;
|
|
|
|
|
|
|
|
uint64_t kbytes = bytes >> 10;
|
|
|
|
double kBytesD = double(kbytes);
|
|
|
|
double x = log(kBytesD)/log(2.0) - 14;
|
|
|
|
|
|
|
|
int32_t capacity = 0;
|
|
|
|
if (x > 0) {
|
|
|
|
capacity = (int32_t)(x * x / 3.0 + x + 2.0 / 3 + 0.1); // 0.1 for rounding
|
|
|
|
if (capacity > 32)
|
|
|
|
capacity = 32;
|
|
|
|
capacity <<= 20;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Result is in bytes.
|
|
|
|
return sAutoMemoryCacheCapacity = capacity;
|
|
|
|
}
|
|
|
|
|
2014-02-05 11:29:54 -08:00
|
|
|
void CacheObserver::SchduleAutoDelete()
|
|
|
|
{
|
|
|
|
// Auto-delete not set
|
|
|
|
if (sAutoDeleteCacheVersion == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Don't autodelete the same version of the cache user has setup
|
|
|
|
// to use.
|
|
|
|
int32_t activeVersion = UseNewCache() ? 1 : 0;
|
|
|
|
if (sAutoDeleteCacheVersion == activeVersion)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CacheStorageService::WipeCacheDirectory(sAutoDeleteCacheVersion);
|
|
|
|
}
|
|
|
|
|
2013-09-20 02:11:25 -07:00
|
|
|
// static
|
|
|
|
bool const CacheObserver::UseNewCache()
|
|
|
|
{
|
2014-02-18 10:06:06 -08:00
|
|
|
uint32_t useNewCache = sUseNewCache;
|
|
|
|
|
|
|
|
if (sUseNewCacheTemp)
|
|
|
|
useNewCache = 1;
|
|
|
|
|
|
|
|
switch (useNewCache) {
|
2013-09-20 02:11:25 -07:00
|
|
|
case 0: // use the old cache backend
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case 1: // use the new cache backend
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-22 02:21:00 -07:00
|
|
|
// static
|
|
|
|
void
|
|
|
|
CacheObserver::SetDiskCacheCapacity(uint32_t aCapacity)
|
|
|
|
{
|
|
|
|
sDiskCacheCapacity = aCapacity >> 10;
|
|
|
|
|
|
|
|
if (!sSelf) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
sSelf->StoreDiskCacheCapacity();
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethod(sSelf, &CacheObserver::StoreDiskCacheCapacity);
|
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CacheObserver::StoreDiskCacheCapacity()
|
|
|
|
{
|
|
|
|
mozilla::Preferences::SetInt("browser.cache.disk.capacity",
|
|
|
|
sDiskCacheCapacity);
|
|
|
|
}
|
|
|
|
|
2014-02-26 15:11:38 -08:00
|
|
|
// static
|
|
|
|
void CacheObserver::ParentDirOverride(nsIFile** aDir)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(!aDir))
|
|
|
|
return;
|
|
|
|
|
|
|
|
*aDir = nullptr;
|
|
|
|
|
|
|
|
if (!sSelf)
|
|
|
|
return;
|
|
|
|
if (!sSelf->mCacheParentDirectoryOverride)
|
|
|
|
return;
|
|
|
|
|
|
|
|
sSelf->mCacheParentDirectoryOverride->Clone(aDir);
|
|
|
|
}
|
|
|
|
|
2013-11-20 14:20:16 -08:00
|
|
|
namespace { // anon
|
|
|
|
|
|
|
|
class CacheStorageEvictHelper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsresult Run(mozIApplicationClearPrivateDataParams* aParams);
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint32_t mAppId;
|
|
|
|
nsresult ClearStorage(bool const aPrivate,
|
|
|
|
bool const aInBrowser,
|
|
|
|
bool const aAnonymous);
|
|
|
|
};
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CacheStorageEvictHelper::Run(mozIApplicationClearPrivateDataParams* aParams)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
rv = aParams->GetAppId(&mAppId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
bool aBrowserOnly;
|
|
|
|
rv = aParams->GetBrowserOnly(&aBrowserOnly);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mAppId != nsILoadContextInfo::UNKNOWN_APP_ID);
|
|
|
|
|
|
|
|
// Clear all [private X anonymous] combinations
|
|
|
|
rv = ClearStorage(false, aBrowserOnly, false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = ClearStorage(false, aBrowserOnly, true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = ClearStorage(true, aBrowserOnly, false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = ClearStorage(true, aBrowserOnly, true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CacheStorageEvictHelper::ClearStorage(bool const aPrivate,
|
|
|
|
bool const aInBrowser,
|
|
|
|
bool const aAnonymous)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsRefPtr<LoadContextInfo> info = GetLoadContextInfo(
|
|
|
|
aPrivate, mAppId, aInBrowser, aAnonymous);
|
|
|
|
|
|
|
|
nsCOMPtr<nsICacheStorage> storage;
|
|
|
|
nsRefPtr<CacheStorageService> service = CacheStorageService::Self();
|
|
|
|
NS_ENSURE_TRUE(service, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
// Clear disk storage
|
|
|
|
rv = service->DiskCacheStorage(info, false, getter_AddRefs(storage));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = storage->AsyncEvictStorage(nullptr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Clear memory storage
|
|
|
|
rv = service->MemoryCacheStorage(info, getter_AddRefs(storage));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = storage->AsyncEvictStorage(nullptr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!aInBrowser) {
|
|
|
|
rv = ClearStorage(aPrivate, true, aAnonymous);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anon
|
|
|
|
|
2014-02-03 16:53:00 -08:00
|
|
|
// static
|
|
|
|
bool const CacheObserver::EntryIsTooBig(int64_t aSize, bool aUsingDisk)
|
|
|
|
{
|
|
|
|
// If custom limit is set, check it.
|
|
|
|
int64_t preferredLimit = aUsingDisk
|
|
|
|
? static_cast<int64_t>(sMaxDiskEntrySize) << 10
|
|
|
|
: static_cast<int64_t>(sMaxMemoryEntrySize) << 10;
|
|
|
|
|
|
|
|
if (preferredLimit != -1 && aSize > preferredLimit)
|
|
|
|
return true;
|
|
|
|
|
2014-04-06 11:45:18 -07:00
|
|
|
// Otherwise (or when in the custom limit), check limit based on the global
|
|
|
|
// limit. It's 1/8 (>> 3) of the respective capacity.
|
2014-02-03 16:53:00 -08:00
|
|
|
int64_t derivedLimit = aUsingDisk
|
2014-04-06 11:45:18 -07:00
|
|
|
? (static_cast<int64_t>(DiskCacheCapacity() >> 3))
|
|
|
|
: (static_cast<int64_t>(MemoryCacheCapacity() >> 3));
|
2014-02-03 16:53:00 -08:00
|
|
|
|
|
|
|
if (aSize > derivedLimit)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-20 02:11:25 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
CacheObserver::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t* aData)
|
2013-09-20 02:11:25 -07:00
|
|
|
{
|
|
|
|
if (!strcmp(aTopic, "prefservice:after-app-defaults")) {
|
|
|
|
CacheFileIOManager::Init();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(aTopic, "profile-do-change")) {
|
2014-02-03 16:52:58 -08:00
|
|
|
AttachToPreferences();
|
2013-09-20 02:11:25 -07:00
|
|
|
CacheFileIOManager::Init();
|
|
|
|
CacheFileIOManager::OnProfile();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-05 11:29:54 -08:00
|
|
|
if (!strcmp(aTopic, "sessionstore-windows-restored")) {
|
|
|
|
SchduleAutoDelete();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-20 02:11:25 -07:00
|
|
|
if (!strcmp(aTopic, "profile-before-change")) {
|
|
|
|
nsRefPtr<CacheStorageService> service = CacheStorageService::Self();
|
|
|
|
if (service)
|
|
|
|
service->Shutdown();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(aTopic, "xpcom-shutdown")) {
|
|
|
|
nsRefPtr<CacheStorageService> service = CacheStorageService::Self();
|
|
|
|
if (service)
|
|
|
|
service->Shutdown();
|
|
|
|
|
|
|
|
CacheFileIOManager::Shutdown();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(aTopic, "last-pb-context-exited")) {
|
|
|
|
nsRefPtr<CacheStorageService> service = CacheStorageService::Self();
|
|
|
|
if (service)
|
|
|
|
service->DropPrivateBrowsingEntries();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-11-20 14:20:16 -08:00
|
|
|
if (!strcmp(aTopic, "webapps-clear-data")) {
|
|
|
|
nsCOMPtr<mozIApplicationClearPrivateDataParams> params =
|
|
|
|
do_QueryInterface(aSubject);
|
|
|
|
if (!params) {
|
|
|
|
NS_ERROR("'webapps-clear-data' notification's subject should be a mozIApplicationClearPrivateDataParams");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheStorageEvictHelper helper;
|
|
|
|
nsresult rv = helper.Run(params);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-20 02:11:25 -07:00
|
|
|
if (!strcmp(aTopic, "memory-pressure")) {
|
|
|
|
nsRefPtr<CacheStorageService> service = CacheStorageService::Self();
|
|
|
|
if (service)
|
|
|
|
service->PurgeFromMemory(nsICacheStorageService::PURGE_EVERYTHING);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-05 11:29:54 -08:00
|
|
|
MOZ_ASSERT(false, "Missing observer handler");
|
2013-09-20 02:11:25 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // net
|
|
|
|
} // mozilla
|