2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2012-05-21 04:12:37 -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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef _nsCacheService_h_
|
|
|
|
#define _nsCacheService_h_
|
|
|
|
|
|
|
|
#include "nsICacheService.h"
|
|
|
|
#include "nsCacheSession.h"
|
|
|
|
#include "nsCacheDevice.h"
|
|
|
|
#include "nsCacheEntry.h"
|
|
|
|
|
|
|
|
#include "prthread.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsTArray.h"
|
2012-06-04 07:12:24 -07:00
|
|
|
#include "nsRefPtrHashtable.h"
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
#include "mozilla/CondVar.h"
|
|
|
|
#include "mozilla/Mutex.h"
|
2012-06-29 16:20:34 -07:00
|
|
|
#include "mozilla/Telemetry.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
class nsCacheRequest;
|
|
|
|
class nsCacheProfilePrefObserver;
|
|
|
|
class nsDiskCacheDevice;
|
|
|
|
class nsMemoryCacheDevice;
|
2007-05-14 13:09:20 -07:00
|
|
|
class nsOfflineCacheDevice;
|
2007-03-22 10:30:00 -07:00
|
|
|
class nsCacheServiceAutoLock;
|
2012-01-06 07:19:10 -08:00
|
|
|
class nsITimer;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsCacheService
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
class nsCacheService : public nsICacheService
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSICACHESERVICE
|
|
|
|
|
|
|
|
nsCacheService();
|
|
|
|
virtual ~nsCacheService();
|
|
|
|
|
|
|
|
// Define a Create method to be used with a factory:
|
2010-06-10 11:11:40 -07:00
|
|
|
static nsresult
|
2007-03-22 10:30:00 -07:00
|
|
|
Create(nsISupports* outer, const nsIID& iid, void* *result);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Methods called by nsCacheSession
|
|
|
|
*/
|
|
|
|
static nsresult OpenCacheEntry(nsCacheSession * session,
|
|
|
|
const nsACString & key,
|
|
|
|
nsCacheAccessMode accessRequested,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool blockingMode,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsICacheListener * listener,
|
|
|
|
nsICacheEntryDescriptor ** result);
|
|
|
|
|
|
|
|
static nsresult EvictEntriesForSession(nsCacheSession * session);
|
|
|
|
|
|
|
|
static nsresult IsStorageEnabledForPolicy(nsCacheStoragePolicy storagePolicy,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool * result);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-22 15:54:20 -07:00
|
|
|
static nsresult DoomEntry(nsCacheSession *session,
|
|
|
|
const nsACString &key,
|
|
|
|
nsICacheListener *listener);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Methods called by nsCacheEntryDescriptor
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void CloseDescriptor(nsCacheEntryDescriptor * descriptor);
|
|
|
|
|
|
|
|
static nsresult GetFileForEntry(nsCacheEntry * entry,
|
|
|
|
nsIFile ** result);
|
|
|
|
|
|
|
|
static nsresult OpenInputStreamForEntry(nsCacheEntry * entry,
|
|
|
|
nsCacheAccessMode mode,
|
|
|
|
PRUint32 offset,
|
|
|
|
nsIInputStream ** result);
|
|
|
|
|
|
|
|
static nsresult OpenOutputStreamForEntry(nsCacheEntry * entry,
|
|
|
|
nsCacheAccessMode mode,
|
|
|
|
PRUint32 offset,
|
|
|
|
nsIOutputStream ** result);
|
|
|
|
|
|
|
|
static nsresult OnDataSizeChange(nsCacheEntry * entry, PRInt32 deltaSize);
|
|
|
|
|
|
|
|
static nsresult SetCacheElement(nsCacheEntry * entry, nsISupports * element);
|
|
|
|
|
|
|
|
static nsresult ValidateEntry(nsCacheEntry * entry);
|
|
|
|
|
2011-12-17 02:30:29 -08:00
|
|
|
static PRInt32 CacheCompressionLevel();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Methods called by any cache classes
|
|
|
|
*/
|
|
|
|
|
|
|
|
static
|
|
|
|
nsCacheService * GlobalInstance() { return gService; }
|
2011-07-18 18:22:36 -07:00
|
|
|
|
2012-04-09 22:52:33 -07:00
|
|
|
static PRInt64 MemoryDeviceSize();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static nsresult DoomEntry(nsCacheEntry * entry);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool IsStorageEnabledForPolicy_Locked(nsCacheStoragePolicy policy);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-04-30 10:57:09 -07:00
|
|
|
/**
|
|
|
|
* Methods called by nsApplicationCacheService
|
|
|
|
*/
|
|
|
|
|
|
|
|
nsresult GetOfflineDevice(nsOfflineCacheDevice ** aDevice);
|
|
|
|
|
2012-06-04 07:12:24 -07:00
|
|
|
/**
|
|
|
|
* Creates an offline cache device that works over a specific profile directory.
|
|
|
|
* A tool to preload offline cache for profiles different from the current
|
|
|
|
* application's profile directory.
|
|
|
|
*/
|
2012-06-05 19:08:30 -07:00
|
|
|
nsresult GetCustomOfflineDevice(nsIFile *aProfileDir,
|
2012-06-04 07:12:24 -07:00
|
|
|
PRInt32 aQuota,
|
|
|
|
nsOfflineCacheDevice **aDevice);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// This method may be called to release an object while the cache service
|
|
|
|
// lock is being held. If a non-null target is specified and the target
|
|
|
|
// does not correspond to the current thread, then the release will be
|
|
|
|
// proxied to the specified target. Otherwise, the object will be added to
|
|
|
|
// the list of objects to be released when the cache service is unlocked.
|
|
|
|
static void ReleaseObject_Locked(nsISupports * object,
|
|
|
|
nsIEventTarget * target = nsnull);
|
|
|
|
|
2010-09-15 11:42:38 -07:00
|
|
|
static nsresult DispatchToCacheIOThread(nsIRunnable* event);
|
|
|
|
|
2011-01-20 13:41:10 -08:00
|
|
|
// Calling this method will block the calling thread until all pending
|
|
|
|
// events on the cache-io thread has finished. The calling thread must
|
|
|
|
// hold the cache-lock
|
|
|
|
static nsresult SyncWithCacheIOThread();
|
|
|
|
|
2010-09-15 11:42:38 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Methods called by nsCacheProfilePrefObserver
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
static void OnProfileShutdown(bool cleanse);
|
2007-03-22 10:30:00 -07:00
|
|
|
static void OnProfileChanged();
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static void SetDiskCacheEnabled(bool enabled);
|
2008-12-14 21:57:24 -08:00
|
|
|
// Sets the disk cache capacity (in kilobytes)
|
2007-03-22 10:30:00 -07:00
|
|
|
static void SetDiskCacheCapacity(PRInt32 capacity);
|
2011-09-13 12:24:01 -07:00
|
|
|
// Set max size for a disk-cache entry (in KB). -1 disables limit up to
|
|
|
|
// 1/8th of disk cache size
|
2011-06-27 05:21:15 -07:00
|
|
|
static void SetDiskCacheMaxEntrySize(PRInt32 maxSize);
|
2011-09-13 12:24:01 -07:00
|
|
|
// Set max size for a memory-cache entry (in kilobytes). -1 disables
|
|
|
|
// limit up to 90% of memory cache size
|
2011-06-27 05:21:15 -07:00
|
|
|
static void SetMemoryCacheMaxEntrySize(PRInt32 maxSize);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static void SetOfflineCacheEnabled(bool enabled);
|
2008-12-14 21:57:24 -08:00
|
|
|
// Sets the offline cache capacity (in kilobytes)
|
2007-03-22 10:30:00 -07:00
|
|
|
static void SetOfflineCacheCapacity(PRInt32 capacity);
|
|
|
|
|
|
|
|
static void SetMemoryCache();
|
|
|
|
|
2011-12-17 02:30:29 -08:00
|
|
|
static void SetCacheCompressionLevel(PRInt32 level);
|
|
|
|
|
2011-10-19 05:35:57 -07:00
|
|
|
// Starts smart cache size computation if disk device is available
|
2011-12-23 19:21:17 -08:00
|
|
|
static nsresult SetDiskSmartSize();
|
2011-10-19 05:35:57 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult Init();
|
|
|
|
void Shutdown();
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
|
|
|
|
static void AssertOwnsLock()
|
|
|
|
{ gService->mLock.AssertCurrentThreadOwns(); }
|
|
|
|
|
2012-05-24 08:31:54 -07:00
|
|
|
static void LeavePrivateBrowsing();
|
|
|
|
|
|
|
|
typedef bool (*DoomCheckFn)(nsCacheEntry* entry);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
private:
|
|
|
|
friend class nsCacheServiceAutoLock;
|
2008-08-26 16:09:02 -07:00
|
|
|
friend class nsOfflineCacheDevice;
|
2010-08-23 18:06:23 -07:00
|
|
|
friend class nsProcessRequestEvent;
|
2010-09-15 11:42:40 -07:00
|
|
|
friend class nsSetSmartSizeEvent;
|
2011-01-20 13:41:10 -08:00
|
|
|
friend class nsBlockOnCacheThreadEvent;
|
2012-01-06 07:19:10 -08:00
|
|
|
friend class nsSetDiskSmartSizeCallback;
|
2012-03-22 15:54:20 -07:00
|
|
|
friend class nsDoomEvent;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal Methods
|
|
|
|
*/
|
|
|
|
|
2012-06-29 16:20:34 -07:00
|
|
|
static void Lock(::mozilla::Telemetry::ID mainThreadLockerID);
|
2007-03-22 10:30:00 -07:00
|
|
|
static void Unlock();
|
|
|
|
|
|
|
|
nsresult CreateDiskDevice();
|
|
|
|
nsresult CreateOfflineDevice();
|
2012-06-05 19:08:30 -07:00
|
|
|
nsresult CreateCustomOfflineDevice(nsIFile *aProfileDir,
|
2012-06-04 07:12:24 -07:00
|
|
|
PRInt32 aQuota,
|
|
|
|
nsOfflineCacheDevice **aDevice);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult CreateMemoryDevice();
|
|
|
|
|
|
|
|
nsresult CreateRequest(nsCacheSession * session,
|
|
|
|
const nsACString & clientKey,
|
|
|
|
nsCacheAccessMode accessRequested,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool blockingMode,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsICacheListener * listener,
|
|
|
|
nsCacheRequest ** request);
|
|
|
|
|
2010-10-25 15:47:40 -07:00
|
|
|
nsresult DoomEntry_Internal(nsCacheEntry * entry,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool doProcessPendingRequests);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult EvictEntriesForClient(const char * clientID,
|
|
|
|
nsCacheStoragePolicy storagePolicy);
|
|
|
|
|
|
|
|
// Notifies request listener asynchronously on the request's thread, and
|
|
|
|
// releases the descriptor on the request's thread. If this method fails,
|
|
|
|
// the descriptor is not released.
|
|
|
|
nsresult NotifyListener(nsCacheRequest * request,
|
|
|
|
nsICacheEntryDescriptor * descriptor,
|
|
|
|
nsCacheAccessMode accessGranted,
|
|
|
|
nsresult error);
|
|
|
|
|
2010-10-25 15:47:40 -07:00
|
|
|
nsresult ActivateEntry(nsCacheRequest * request,
|
|
|
|
nsCacheEntry ** entry,
|
|
|
|
nsCacheEntry ** doomedEntry);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCacheDevice * EnsureEntryHasDevice(nsCacheEntry * entry);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
nsCacheEntry * SearchCacheDevices(nsCString * key, nsCacheStoragePolicy policy, bool *collision);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void DeactivateEntry(nsCacheEntry * entry);
|
|
|
|
|
|
|
|
nsresult ProcessRequest(nsCacheRequest * request,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool calledFromOpenCacheEntry,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsICacheEntryDescriptor ** result);
|
|
|
|
|
|
|
|
nsresult ProcessPendingRequests(nsCacheEntry * entry);
|
|
|
|
|
|
|
|
void ClearPendingRequests(nsCacheEntry * entry);
|
|
|
|
void ClearDoomList(void);
|
|
|
|
void ClearActiveEntries(void);
|
2012-05-24 08:31:54 -07:00
|
|
|
void DoomActiveEntries(DoomCheckFn check);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static
|
2008-10-10 08:04:34 -07:00
|
|
|
PLDHashOperator DeactivateAndClearEntry(PLDHashTable * table,
|
|
|
|
PLDHashEntryHdr * hdr,
|
|
|
|
PRUint32 number,
|
|
|
|
void * arg);
|
2007-03-22 10:30:00 -07:00
|
|
|
static
|
2008-10-10 08:04:34 -07:00
|
|
|
PLDHashOperator RemoveActiveEntry(PLDHashTable * table,
|
|
|
|
PLDHashEntryHdr * hdr,
|
|
|
|
PRUint32 number,
|
|
|
|
void * arg);
|
2012-06-04 07:12:24 -07:00
|
|
|
|
|
|
|
static
|
|
|
|
PLDHashOperator ShutdownCustomCacheDeviceEnum(const nsAString& aProfileDir,
|
|
|
|
nsRefPtr<nsOfflineCacheDevice>& aDevice,
|
|
|
|
void* aUserArg);
|
2007-03-22 10:30:00 -07:00
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
void LogCacheStatistics();
|
|
|
|
#endif
|
|
|
|
|
2012-01-06 07:19:10 -08:00
|
|
|
nsresult SetDiskSmartSize_Locked();
|
2011-10-19 05:35:57 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Data Members
|
|
|
|
*/
|
|
|
|
|
|
|
|
static nsCacheService * gService; // there can be only one...
|
|
|
|
|
|
|
|
nsCacheProfilePrefObserver * mObserver;
|
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
mozilla::Mutex mLock;
|
|
|
|
mozilla::CondVar mCondVar;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-23 18:06:23 -07:00
|
|
|
nsCOMPtr<nsIThread> mCacheIOThread;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsTArray<nsISupports*> mDoomedObjects;
|
2012-01-06 07:19:10 -08:00
|
|
|
nsCOMPtr<nsITimer> mSmartSizeTimer;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mInitialized;
|
2012-04-12 09:24:57 -07:00
|
|
|
bool mClearingEntries;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mEnableMemoryDevice;
|
|
|
|
bool mEnableDiskDevice;
|
|
|
|
bool mEnableOfflineDevice;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsMemoryCacheDevice * mMemoryDevice;
|
|
|
|
nsDiskCacheDevice * mDiskDevice;
|
2007-05-14 13:09:20 -07:00
|
|
|
nsOfflineCacheDevice * mOfflineDevice;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-04 07:12:24 -07:00
|
|
|
nsRefPtrHashtable<nsStringHashKey, nsOfflineCacheDevice> mCustomOfflineDevices;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCacheEntryHashTable mActiveEntries;
|
|
|
|
PRCList mDoomedEntries;
|
|
|
|
|
|
|
|
// stats
|
|
|
|
|
|
|
|
PRUint32 mTotalEntries;
|
|
|
|
PRUint32 mCacheHits;
|
|
|
|
PRUint32 mCacheMisses;
|
|
|
|
PRUint32 mMaxKeyLength;
|
|
|
|
PRUint32 mMaxDataSize;
|
|
|
|
PRUint32 mMaxMetaSize;
|
|
|
|
|
|
|
|
// Unexpected error totals
|
|
|
|
PRUint32 mDeactivateFailures;
|
|
|
|
PRUint32 mDeactivatedUnboundEntries;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsCacheServiceAutoLock
|
|
|
|
******************************************************************************/
|
|
|
|
|
2012-06-29 16:20:34 -07:00
|
|
|
#define LOCK_TELEM(x) \
|
|
|
|
(::mozilla::Telemetry::CACHE_SERVICE_LOCK_WAIT_MAINTHREAD_##x)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Instantiate this class to acquire the cache service lock for a particular
|
|
|
|
// execution scope.
|
|
|
|
class nsCacheServiceAutoLock {
|
|
|
|
public:
|
2012-06-29 16:20:34 -07:00
|
|
|
nsCacheServiceAutoLock(mozilla::Telemetry::ID mainThreadLockerID) {
|
|
|
|
nsCacheService::Lock(mainThreadLockerID);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
~nsCacheServiceAutoLock() {
|
|
|
|
nsCacheService::Unlock();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _nsCacheService_h_
|