Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "CacheObserver.h" |
michael@0 | 6 | |
michael@0 | 7 | #include "CacheStorageService.h" |
michael@0 | 8 | #include "CacheFileIOManager.h" |
michael@0 | 9 | #include "LoadContextInfo.h" |
michael@0 | 10 | #include "nsICacheStorage.h" |
michael@0 | 11 | #include "nsIObserverService.h" |
michael@0 | 12 | #include "mozIApplicationClearPrivateDataParams.h" |
michael@0 | 13 | #include "mozilla/Services.h" |
michael@0 | 14 | #include "mozilla/Preferences.h" |
michael@0 | 15 | #include "nsServiceManagerUtils.h" |
michael@0 | 16 | #include "prsystem.h" |
michael@0 | 17 | #include <time.h> |
michael@0 | 18 | #include <math.h> |
michael@0 | 19 | |
michael@0 | 20 | namespace mozilla { |
michael@0 | 21 | namespace net { |
michael@0 | 22 | |
michael@0 | 23 | CacheObserver* CacheObserver::sSelf = nullptr; |
michael@0 | 24 | |
michael@0 | 25 | static uint32_t const kDefaultUseNewCache = 0; // Don't use the new cache by default |
michael@0 | 26 | uint32_t CacheObserver::sUseNewCache = kDefaultUseNewCache; |
michael@0 | 27 | |
michael@0 | 28 | static bool sUseNewCacheTemp = false; // Temp trigger to not lose early adopters |
michael@0 | 29 | |
michael@0 | 30 | static int32_t const kAutoDeleteCacheVersion = -1; // Auto-delete off by default |
michael@0 | 31 | static int32_t sAutoDeleteCacheVersion = kAutoDeleteCacheVersion; |
michael@0 | 32 | |
michael@0 | 33 | static int32_t const kDefaultHalfLifeExperiment = -1; // Disabled |
michael@0 | 34 | int32_t CacheObserver::sHalfLifeExperiment = kDefaultHalfLifeExperiment; |
michael@0 | 35 | |
michael@0 | 36 | static uint32_t const kDefaultHalfLifeHours = 6; // 6 hours |
michael@0 | 37 | uint32_t CacheObserver::sHalfLifeHours = kDefaultHalfLifeHours; |
michael@0 | 38 | |
michael@0 | 39 | static bool const kDefaultUseDiskCache = true; |
michael@0 | 40 | bool CacheObserver::sUseDiskCache = kDefaultUseDiskCache; |
michael@0 | 41 | |
michael@0 | 42 | static bool const kDefaultUseMemoryCache = true; |
michael@0 | 43 | bool CacheObserver::sUseMemoryCache = kDefaultUseMemoryCache; |
michael@0 | 44 | |
michael@0 | 45 | static uint32_t const kDefaultMetadataMemoryLimit = 250; // 0.25 MB |
michael@0 | 46 | uint32_t CacheObserver::sMetadataMemoryLimit = kDefaultMetadataMemoryLimit; |
michael@0 | 47 | |
michael@0 | 48 | static int32_t const kDefaultMemoryCacheCapacity = -1; // autodetect |
michael@0 | 49 | int32_t CacheObserver::sMemoryCacheCapacity = kDefaultMemoryCacheCapacity; |
michael@0 | 50 | // Cache of the calculated memory capacity based on the system memory size |
michael@0 | 51 | int32_t CacheObserver::sAutoMemoryCacheCapacity = -1; |
michael@0 | 52 | |
michael@0 | 53 | static uint32_t const kDefaultDiskCacheCapacity = 250 * 1024; // 250 MB |
michael@0 | 54 | uint32_t CacheObserver::sDiskCacheCapacity = kDefaultDiskCacheCapacity; |
michael@0 | 55 | |
michael@0 | 56 | static bool const kDefaultSmartCacheSizeEnabled = false; |
michael@0 | 57 | bool CacheObserver::sSmartCacheSizeEnabled = kDefaultSmartCacheSizeEnabled; |
michael@0 | 58 | |
michael@0 | 59 | static uint32_t const kDefaultMaxMemoryEntrySize = 4 * 1024; // 4 MB |
michael@0 | 60 | uint32_t CacheObserver::sMaxMemoryEntrySize = kDefaultMaxMemoryEntrySize; |
michael@0 | 61 | |
michael@0 | 62 | static uint32_t const kDefaultMaxDiskEntrySize = 50 * 1024; // 50 MB |
michael@0 | 63 | uint32_t CacheObserver::sMaxDiskEntrySize = kDefaultMaxDiskEntrySize; |
michael@0 | 64 | |
michael@0 | 65 | static uint32_t const kDefaultCompressionLevel = 1; |
michael@0 | 66 | uint32_t CacheObserver::sCompressionLevel = kDefaultCompressionLevel; |
michael@0 | 67 | |
michael@0 | 68 | static bool kDefaultSanitizeOnShutdown = false; |
michael@0 | 69 | bool CacheObserver::sSanitizeOnShutdown = kDefaultSanitizeOnShutdown; |
michael@0 | 70 | |
michael@0 | 71 | static bool kDefaultClearCacheOnShutdown = false; |
michael@0 | 72 | bool CacheObserver::sClearCacheOnShutdown = kDefaultClearCacheOnShutdown; |
michael@0 | 73 | |
michael@0 | 74 | NS_IMPL_ISUPPORTS(CacheObserver, |
michael@0 | 75 | nsIObserver, |
michael@0 | 76 | nsISupportsWeakReference) |
michael@0 | 77 | |
michael@0 | 78 | // static |
michael@0 | 79 | nsresult |
michael@0 | 80 | CacheObserver::Init() |
michael@0 | 81 | { |
michael@0 | 82 | if (sSelf) { |
michael@0 | 83 | return NS_OK; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService(); |
michael@0 | 87 | if (!obs) { |
michael@0 | 88 | return NS_ERROR_UNEXPECTED; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | sSelf = new CacheObserver(); |
michael@0 | 92 | NS_ADDREF(sSelf); |
michael@0 | 93 | |
michael@0 | 94 | obs->AddObserver(sSelf, "prefservice:after-app-defaults", true); |
michael@0 | 95 | obs->AddObserver(sSelf, "profile-do-change", true); |
michael@0 | 96 | obs->AddObserver(sSelf, "sessionstore-windows-restored", true); |
michael@0 | 97 | obs->AddObserver(sSelf, "profile-before-change", true); |
michael@0 | 98 | obs->AddObserver(sSelf, "xpcom-shutdown", true); |
michael@0 | 99 | obs->AddObserver(sSelf, "last-pb-context-exited", true); |
michael@0 | 100 | obs->AddObserver(sSelf, "webapps-clear-data", true); |
michael@0 | 101 | obs->AddObserver(sSelf, "memory-pressure", true); |
michael@0 | 102 | |
michael@0 | 103 | return NS_OK; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | // static |
michael@0 | 107 | nsresult |
michael@0 | 108 | CacheObserver::Shutdown() |
michael@0 | 109 | { |
michael@0 | 110 | if (!sSelf) { |
michael@0 | 111 | return NS_ERROR_NOT_INITIALIZED; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | NS_RELEASE(sSelf); |
michael@0 | 115 | return NS_OK; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | void |
michael@0 | 119 | CacheObserver::AttachToPreferences() |
michael@0 | 120 | { |
michael@0 | 121 | sAutoDeleteCacheVersion = mozilla::Preferences::GetInt( |
michael@0 | 122 | "browser.cache.auto_delete_cache_version", kAutoDeleteCacheVersion); |
michael@0 | 123 | |
michael@0 | 124 | mozilla::Preferences::AddUintVarCache( |
michael@0 | 125 | &sUseNewCache, "browser.cache.use_new_backend", kDefaultUseNewCache); |
michael@0 | 126 | mozilla::Preferences::AddBoolVarCache( |
michael@0 | 127 | &sUseNewCacheTemp, "browser.cache.use_new_backend_temp", false); |
michael@0 | 128 | |
michael@0 | 129 | mozilla::Preferences::AddBoolVarCache( |
michael@0 | 130 | &sUseDiskCache, "browser.cache.disk.enable", kDefaultUseDiskCache); |
michael@0 | 131 | mozilla::Preferences::AddBoolVarCache( |
michael@0 | 132 | &sUseMemoryCache, "browser.cache.memory.enable", kDefaultUseMemoryCache); |
michael@0 | 133 | |
michael@0 | 134 | mozilla::Preferences::AddUintVarCache( |
michael@0 | 135 | &sMetadataMemoryLimit, "browser.cache.disk.metadata_memory_limit", kDefaultMetadataMemoryLimit); |
michael@0 | 136 | |
michael@0 | 137 | mozilla::Preferences::AddUintVarCache( |
michael@0 | 138 | &sDiskCacheCapacity, "browser.cache.disk.capacity", kDefaultDiskCacheCapacity); |
michael@0 | 139 | mozilla::Preferences::AddBoolVarCache( |
michael@0 | 140 | &sSmartCacheSizeEnabled, "browser.cache.disk.smart_size.enabled", kDefaultSmartCacheSizeEnabled); |
michael@0 | 141 | mozilla::Preferences::AddIntVarCache( |
michael@0 | 142 | &sMemoryCacheCapacity, "browser.cache.memory.capacity", kDefaultMemoryCacheCapacity); |
michael@0 | 143 | |
michael@0 | 144 | mozilla::Preferences::AddUintVarCache( |
michael@0 | 145 | &sMaxDiskEntrySize, "browser.cache.disk.max_entry_size", kDefaultMaxDiskEntrySize); |
michael@0 | 146 | mozilla::Preferences::AddUintVarCache( |
michael@0 | 147 | &sMaxMemoryEntrySize, "browser.cache.memory.max_entry_size", kDefaultMaxMemoryEntrySize); |
michael@0 | 148 | |
michael@0 | 149 | // http://mxr.mozilla.org/mozilla-central/source/netwerk/cache/nsCacheEntryDescriptor.cpp#367 |
michael@0 | 150 | mozilla::Preferences::AddUintVarCache( |
michael@0 | 151 | &sCompressionLevel, "browser.cache.compression_level", kDefaultCompressionLevel); |
michael@0 | 152 | |
michael@0 | 153 | mozilla::Preferences::GetComplex( |
michael@0 | 154 | "browser.cache.disk.parent_directory", NS_GET_IID(nsIFile), |
michael@0 | 155 | getter_AddRefs(mCacheParentDirectoryOverride)); |
michael@0 | 156 | |
michael@0 | 157 | // First check the default value. If it is at -1, the experient |
michael@0 | 158 | // is turned off. If it is at 0, then use the user pref value |
michael@0 | 159 | // instead. |
michael@0 | 160 | sHalfLifeExperiment = mozilla::Preferences::GetDefaultInt( |
michael@0 | 161 | "browser.cache.frecency_experiment", kDefaultHalfLifeExperiment); |
michael@0 | 162 | |
michael@0 | 163 | if (sHalfLifeExperiment == 0) { |
michael@0 | 164 | // Default preferences indicate we want to run the experiment, |
michael@0 | 165 | // hence read the user value. |
michael@0 | 166 | sHalfLifeExperiment = mozilla::Preferences::GetInt( |
michael@0 | 167 | "browser.cache.frecency_experiment", sHalfLifeExperiment); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | if (sHalfLifeExperiment == 0) { |
michael@0 | 171 | // The experiment has not yet been initialized but is engaged, do |
michael@0 | 172 | // the initialization now. |
michael@0 | 173 | srand(time(NULL)); |
michael@0 | 174 | sHalfLifeExperiment = (rand() % 4) + 1; |
michael@0 | 175 | // Store the experiemnt value, since we need it not to change between |
michael@0 | 176 | // browser sessions. |
michael@0 | 177 | mozilla::Preferences::SetInt( |
michael@0 | 178 | "browser.cache.frecency_experiment", sHalfLifeExperiment); |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | switch (sHalfLifeExperiment) { |
michael@0 | 182 | case 1: // The experiment is engaged |
michael@0 | 183 | sHalfLifeHours = 6; |
michael@0 | 184 | break; |
michael@0 | 185 | case 2: |
michael@0 | 186 | sHalfLifeHours = 24; |
michael@0 | 187 | break; |
michael@0 | 188 | case 3: |
michael@0 | 189 | sHalfLifeHours = 7 * 24; |
michael@0 | 190 | break; |
michael@0 | 191 | case 4: |
michael@0 | 192 | sHalfLifeHours = 50 * 24; |
michael@0 | 193 | break; |
michael@0 | 194 | |
michael@0 | 195 | case -1: |
michael@0 | 196 | default: // The experiment is off or broken |
michael@0 | 197 | sHalfLifeExperiment = -1; |
michael@0 | 198 | sHalfLifeHours = std::max(1U, std::min(1440U, mozilla::Preferences::GetUint( |
michael@0 | 199 | "browser.cache.frecency_half_life_hours", kDefaultHalfLifeHours))); |
michael@0 | 200 | break; |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | mozilla::Preferences::AddBoolVarCache( |
michael@0 | 204 | &sSanitizeOnShutdown, "privacy.sanitize.sanitizeOnShutdown", kDefaultSanitizeOnShutdown); |
michael@0 | 205 | mozilla::Preferences::AddBoolVarCache( |
michael@0 | 206 | &sClearCacheOnShutdown, "privacy.clearOnShutdown.cache", kDefaultClearCacheOnShutdown); |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | // static |
michael@0 | 210 | uint32_t const CacheObserver::MemoryCacheCapacity() |
michael@0 | 211 | { |
michael@0 | 212 | if (sMemoryCacheCapacity >= 0) |
michael@0 | 213 | return sMemoryCacheCapacity << 10; |
michael@0 | 214 | |
michael@0 | 215 | if (sAutoMemoryCacheCapacity != -1) |
michael@0 | 216 | return sAutoMemoryCacheCapacity; |
michael@0 | 217 | |
michael@0 | 218 | static uint64_t bytes = PR_GetPhysicalMemorySize(); |
michael@0 | 219 | // If getting the physical memory failed, arbitrarily assume |
michael@0 | 220 | // 32 MB of RAM. We use a low default to have a reasonable |
michael@0 | 221 | // size on all the devices we support. |
michael@0 | 222 | if (bytes == 0) |
michael@0 | 223 | bytes = 32 * 1024 * 1024; |
michael@0 | 224 | |
michael@0 | 225 | // Conversion from unsigned int64_t to double doesn't work on all platforms. |
michael@0 | 226 | // We need to truncate the value at INT64_MAX to make sure we don't |
michael@0 | 227 | // overflow. |
michael@0 | 228 | if (bytes > INT64_MAX) |
michael@0 | 229 | bytes = INT64_MAX; |
michael@0 | 230 | |
michael@0 | 231 | uint64_t kbytes = bytes >> 10; |
michael@0 | 232 | double kBytesD = double(kbytes); |
michael@0 | 233 | double x = log(kBytesD)/log(2.0) - 14; |
michael@0 | 234 | |
michael@0 | 235 | int32_t capacity = 0; |
michael@0 | 236 | if (x > 0) { |
michael@0 | 237 | capacity = (int32_t)(x * x / 3.0 + x + 2.0 / 3 + 0.1); // 0.1 for rounding |
michael@0 | 238 | if (capacity > 32) |
michael@0 | 239 | capacity = 32; |
michael@0 | 240 | capacity <<= 20; |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | // Result is in bytes. |
michael@0 | 244 | return sAutoMemoryCacheCapacity = capacity; |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | void CacheObserver::SchduleAutoDelete() |
michael@0 | 248 | { |
michael@0 | 249 | // Auto-delete not set |
michael@0 | 250 | if (sAutoDeleteCacheVersion == -1) |
michael@0 | 251 | return; |
michael@0 | 252 | |
michael@0 | 253 | // Don't autodelete the same version of the cache user has setup |
michael@0 | 254 | // to use. |
michael@0 | 255 | int32_t activeVersion = UseNewCache() ? 1 : 0; |
michael@0 | 256 | if (sAutoDeleteCacheVersion == activeVersion) |
michael@0 | 257 | return; |
michael@0 | 258 | |
michael@0 | 259 | CacheStorageService::WipeCacheDirectory(sAutoDeleteCacheVersion); |
michael@0 | 260 | } |
michael@0 | 261 | |
michael@0 | 262 | // static |
michael@0 | 263 | bool const CacheObserver::UseNewCache() |
michael@0 | 264 | { |
michael@0 | 265 | uint32_t useNewCache = sUseNewCache; |
michael@0 | 266 | |
michael@0 | 267 | if (sUseNewCacheTemp) |
michael@0 | 268 | useNewCache = 1; |
michael@0 | 269 | |
michael@0 | 270 | switch (useNewCache) { |
michael@0 | 271 | case 0: // use the old cache backend |
michael@0 | 272 | return false; |
michael@0 | 273 | |
michael@0 | 274 | case 1: // use the new cache backend |
michael@0 | 275 | return true; |
michael@0 | 276 | } |
michael@0 | 277 | |
michael@0 | 278 | return true; |
michael@0 | 279 | } |
michael@0 | 280 | |
michael@0 | 281 | // static |
michael@0 | 282 | void |
michael@0 | 283 | CacheObserver::SetDiskCacheCapacity(uint32_t aCapacity) |
michael@0 | 284 | { |
michael@0 | 285 | sDiskCacheCapacity = aCapacity >> 10; |
michael@0 | 286 | |
michael@0 | 287 | if (!sSelf) { |
michael@0 | 288 | return; |
michael@0 | 289 | } |
michael@0 | 290 | |
michael@0 | 291 | if (NS_IsMainThread()) { |
michael@0 | 292 | sSelf->StoreDiskCacheCapacity(); |
michael@0 | 293 | } else { |
michael@0 | 294 | nsCOMPtr<nsIRunnable> event = |
michael@0 | 295 | NS_NewRunnableMethod(sSelf, &CacheObserver::StoreDiskCacheCapacity); |
michael@0 | 296 | NS_DispatchToMainThread(event); |
michael@0 | 297 | } |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | void |
michael@0 | 301 | CacheObserver::StoreDiskCacheCapacity() |
michael@0 | 302 | { |
michael@0 | 303 | mozilla::Preferences::SetInt("browser.cache.disk.capacity", |
michael@0 | 304 | sDiskCacheCapacity); |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | // static |
michael@0 | 308 | void CacheObserver::ParentDirOverride(nsIFile** aDir) |
michael@0 | 309 | { |
michael@0 | 310 | if (NS_WARN_IF(!aDir)) |
michael@0 | 311 | return; |
michael@0 | 312 | |
michael@0 | 313 | *aDir = nullptr; |
michael@0 | 314 | |
michael@0 | 315 | if (!sSelf) |
michael@0 | 316 | return; |
michael@0 | 317 | if (!sSelf->mCacheParentDirectoryOverride) |
michael@0 | 318 | return; |
michael@0 | 319 | |
michael@0 | 320 | sSelf->mCacheParentDirectoryOverride->Clone(aDir); |
michael@0 | 321 | } |
michael@0 | 322 | |
michael@0 | 323 | namespace { // anon |
michael@0 | 324 | |
michael@0 | 325 | class CacheStorageEvictHelper |
michael@0 | 326 | { |
michael@0 | 327 | public: |
michael@0 | 328 | nsresult Run(mozIApplicationClearPrivateDataParams* aParams); |
michael@0 | 329 | |
michael@0 | 330 | private: |
michael@0 | 331 | uint32_t mAppId; |
michael@0 | 332 | nsresult ClearStorage(bool const aPrivate, |
michael@0 | 333 | bool const aInBrowser, |
michael@0 | 334 | bool const aAnonymous); |
michael@0 | 335 | }; |
michael@0 | 336 | |
michael@0 | 337 | nsresult |
michael@0 | 338 | CacheStorageEvictHelper::Run(mozIApplicationClearPrivateDataParams* aParams) |
michael@0 | 339 | { |
michael@0 | 340 | nsresult rv; |
michael@0 | 341 | |
michael@0 | 342 | rv = aParams->GetAppId(&mAppId); |
michael@0 | 343 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 344 | |
michael@0 | 345 | bool aBrowserOnly; |
michael@0 | 346 | rv = aParams->GetBrowserOnly(&aBrowserOnly); |
michael@0 | 347 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 348 | |
michael@0 | 349 | MOZ_ASSERT(mAppId != nsILoadContextInfo::UNKNOWN_APP_ID); |
michael@0 | 350 | |
michael@0 | 351 | // Clear all [private X anonymous] combinations |
michael@0 | 352 | rv = ClearStorage(false, aBrowserOnly, false); |
michael@0 | 353 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 354 | rv = ClearStorage(false, aBrowserOnly, true); |
michael@0 | 355 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 356 | rv = ClearStorage(true, aBrowserOnly, false); |
michael@0 | 357 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 358 | rv = ClearStorage(true, aBrowserOnly, true); |
michael@0 | 359 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 360 | |
michael@0 | 361 | return NS_OK; |
michael@0 | 362 | } |
michael@0 | 363 | |
michael@0 | 364 | nsresult |
michael@0 | 365 | CacheStorageEvictHelper::ClearStorage(bool const aPrivate, |
michael@0 | 366 | bool const aInBrowser, |
michael@0 | 367 | bool const aAnonymous) |
michael@0 | 368 | { |
michael@0 | 369 | nsresult rv; |
michael@0 | 370 | |
michael@0 | 371 | nsRefPtr<LoadContextInfo> info = GetLoadContextInfo( |
michael@0 | 372 | aPrivate, mAppId, aInBrowser, aAnonymous); |
michael@0 | 373 | |
michael@0 | 374 | nsCOMPtr<nsICacheStorage> storage; |
michael@0 | 375 | nsRefPtr<CacheStorageService> service = CacheStorageService::Self(); |
michael@0 | 376 | NS_ENSURE_TRUE(service, NS_ERROR_FAILURE); |
michael@0 | 377 | |
michael@0 | 378 | // Clear disk storage |
michael@0 | 379 | rv = service->DiskCacheStorage(info, false, getter_AddRefs(storage)); |
michael@0 | 380 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 381 | rv = storage->AsyncEvictStorage(nullptr); |
michael@0 | 382 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 383 | |
michael@0 | 384 | // Clear memory storage |
michael@0 | 385 | rv = service->MemoryCacheStorage(info, getter_AddRefs(storage)); |
michael@0 | 386 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 387 | rv = storage->AsyncEvictStorage(nullptr); |
michael@0 | 388 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 389 | |
michael@0 | 390 | if (!aInBrowser) { |
michael@0 | 391 | rv = ClearStorage(aPrivate, true, aAnonymous); |
michael@0 | 392 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 393 | } |
michael@0 | 394 | |
michael@0 | 395 | return NS_OK; |
michael@0 | 396 | } |
michael@0 | 397 | |
michael@0 | 398 | } // anon |
michael@0 | 399 | |
michael@0 | 400 | // static |
michael@0 | 401 | bool const CacheObserver::EntryIsTooBig(int64_t aSize, bool aUsingDisk) |
michael@0 | 402 | { |
michael@0 | 403 | // If custom limit is set, check it. |
michael@0 | 404 | int64_t preferredLimit = aUsingDisk |
michael@0 | 405 | ? static_cast<int64_t>(sMaxDiskEntrySize) << 10 |
michael@0 | 406 | : static_cast<int64_t>(sMaxMemoryEntrySize) << 10; |
michael@0 | 407 | |
michael@0 | 408 | if (preferredLimit != -1 && aSize > preferredLimit) |
michael@0 | 409 | return true; |
michael@0 | 410 | |
michael@0 | 411 | // Otherwise (or when in the custom limit), check limit based on the global |
michael@0 | 412 | // limit. It's 1/8 (>> 3) of the respective capacity. |
michael@0 | 413 | int64_t derivedLimit = aUsingDisk |
michael@0 | 414 | ? (static_cast<int64_t>(DiskCacheCapacity() >> 3)) |
michael@0 | 415 | : (static_cast<int64_t>(MemoryCacheCapacity() >> 3)); |
michael@0 | 416 | |
michael@0 | 417 | if (aSize > derivedLimit) |
michael@0 | 418 | return true; |
michael@0 | 419 | |
michael@0 | 420 | return false; |
michael@0 | 421 | } |
michael@0 | 422 | |
michael@0 | 423 | NS_IMETHODIMP |
michael@0 | 424 | CacheObserver::Observe(nsISupports* aSubject, |
michael@0 | 425 | const char* aTopic, |
michael@0 | 426 | const char16_t* aData) |
michael@0 | 427 | { |
michael@0 | 428 | if (!strcmp(aTopic, "prefservice:after-app-defaults")) { |
michael@0 | 429 | CacheFileIOManager::Init(); |
michael@0 | 430 | return NS_OK; |
michael@0 | 431 | } |
michael@0 | 432 | |
michael@0 | 433 | if (!strcmp(aTopic, "profile-do-change")) { |
michael@0 | 434 | AttachToPreferences(); |
michael@0 | 435 | CacheFileIOManager::Init(); |
michael@0 | 436 | CacheFileIOManager::OnProfile(); |
michael@0 | 437 | return NS_OK; |
michael@0 | 438 | } |
michael@0 | 439 | |
michael@0 | 440 | if (!strcmp(aTopic, "sessionstore-windows-restored")) { |
michael@0 | 441 | SchduleAutoDelete(); |
michael@0 | 442 | return NS_OK; |
michael@0 | 443 | } |
michael@0 | 444 | |
michael@0 | 445 | if (!strcmp(aTopic, "profile-before-change")) { |
michael@0 | 446 | nsRefPtr<CacheStorageService> service = CacheStorageService::Self(); |
michael@0 | 447 | if (service) |
michael@0 | 448 | service->Shutdown(); |
michael@0 | 449 | |
michael@0 | 450 | return NS_OK; |
michael@0 | 451 | } |
michael@0 | 452 | |
michael@0 | 453 | if (!strcmp(aTopic, "xpcom-shutdown")) { |
michael@0 | 454 | nsRefPtr<CacheStorageService> service = CacheStorageService::Self(); |
michael@0 | 455 | if (service) |
michael@0 | 456 | service->Shutdown(); |
michael@0 | 457 | |
michael@0 | 458 | CacheFileIOManager::Shutdown(); |
michael@0 | 459 | return NS_OK; |
michael@0 | 460 | } |
michael@0 | 461 | |
michael@0 | 462 | if (!strcmp(aTopic, "last-pb-context-exited")) { |
michael@0 | 463 | nsRefPtr<CacheStorageService> service = CacheStorageService::Self(); |
michael@0 | 464 | if (service) |
michael@0 | 465 | service->DropPrivateBrowsingEntries(); |
michael@0 | 466 | |
michael@0 | 467 | return NS_OK; |
michael@0 | 468 | } |
michael@0 | 469 | |
michael@0 | 470 | if (!strcmp(aTopic, "webapps-clear-data")) { |
michael@0 | 471 | nsCOMPtr<mozIApplicationClearPrivateDataParams> params = |
michael@0 | 472 | do_QueryInterface(aSubject); |
michael@0 | 473 | if (!params) { |
michael@0 | 474 | NS_ERROR("'webapps-clear-data' notification's subject should be a mozIApplicationClearPrivateDataParams"); |
michael@0 | 475 | return NS_ERROR_UNEXPECTED; |
michael@0 | 476 | } |
michael@0 | 477 | |
michael@0 | 478 | CacheStorageEvictHelper helper; |
michael@0 | 479 | nsresult rv = helper.Run(params); |
michael@0 | 480 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 481 | |
michael@0 | 482 | return NS_OK; |
michael@0 | 483 | } |
michael@0 | 484 | |
michael@0 | 485 | if (!strcmp(aTopic, "memory-pressure")) { |
michael@0 | 486 | nsRefPtr<CacheStorageService> service = CacheStorageService::Self(); |
michael@0 | 487 | if (service) |
michael@0 | 488 | service->PurgeFromMemory(nsICacheStorageService::PURGE_EVERYTHING); |
michael@0 | 489 | |
michael@0 | 490 | return NS_OK; |
michael@0 | 491 | } |
michael@0 | 492 | |
michael@0 | 493 | MOZ_ASSERT(false, "Missing observer handler"); |
michael@0 | 494 | return NS_OK; |
michael@0 | 495 | } |
michael@0 | 496 | |
michael@0 | 497 | } // net |
michael@0 | 498 | } // mozilla |