2001-02-23 16:18:01 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2004-04-19 02:01:16 +04:00
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is nsCacheService.cpp, released
|
|
|
|
* February 10, 2001.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2001
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Gordon Sheridan, 10-February-2001
|
2008-10-25 14:26:20 +04:00
|
|
|
* Michael Ventnor <m.ventnor@gmail.com>
|
|
|
|
* Ehsan Akhgari <ehsan.akhgari@gmail.com>
|
2004-04-19 02:01:16 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2003-04-23 07:47:43 +04:00
|
|
|
#include "necko-config.h"
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2001-03-21 01:42:03 +03:00
|
|
|
#include "nsCache.h"
|
2001-02-23 16:18:01 +03:00
|
|
|
#include "nsCacheService.h"
|
2001-03-21 01:42:03 +03:00
|
|
|
#include "nsCacheRequest.h"
|
2001-02-26 17:55:58 +03:00
|
|
|
#include "nsCacheEntry.h"
|
|
|
|
#include "nsCacheEntryDescriptor.h"
|
2001-02-23 16:18:01 +03:00
|
|
|
#include "nsCacheDevice.h"
|
2001-03-21 01:42:03 +03:00
|
|
|
#include "nsMemoryCacheDevice.h"
|
|
|
|
#include "nsICacheVisitor.h"
|
2007-05-15 00:09:20 +04:00
|
|
|
#include "nsDiskCacheDevice.h"
|
2001-03-21 01:42:03 +03:00
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
#ifdef NECKO_OFFLINE_CACHE
|
2004-10-20 09:00:23 +04:00
|
|
|
#include "nsDiskCacheDeviceSQL.h"
|
|
|
|
#endif
|
|
|
|
|
2001-03-13 04:59:43 +03:00
|
|
|
#include "nsIObserverService.h"
|
2001-04-26 22:41:11 +04:00
|
|
|
#include "nsIPrefService.h"
|
|
|
|
#include "nsIPrefBranch.h"
|
2005-02-25 23:46:35 +03:00
|
|
|
#include "nsIPrefBranch2.h"
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsILocalFile.h"
|
2007-03-14 04:52:07 +03:00
|
|
|
#include "nsIOService.h"
|
2001-06-28 05:30:26 +04:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2006-06-28 03:13:48 +04:00
|
|
|
#include "nsProxyRelease.h"
|
2001-09-18 00:47:09 +04:00
|
|
|
#include "nsVoidArray.h"
|
2005-04-26 04:37:23 +04:00
|
|
|
#include "nsDeleteDir.h"
|
2008-10-25 14:26:20 +04:00
|
|
|
#include "nsIPrivateBrowsingService.h"
|
|
|
|
#include "nsNetCID.h"
|
2005-06-16 03:37:29 +04:00
|
|
|
#include <math.h> // for log()
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
#include "mozilla/Services.h"
|
2001-03-29 09:54:58 +04:00
|
|
|
|
2010-05-20 03:22:19 +04:00
|
|
|
#include "mozilla/FunctionTimer.h"
|
2001-03-29 09:54:58 +04:00
|
|
|
|
|
|
|
/******************************************************************************
|
2001-06-28 05:30:26 +04:00
|
|
|
* nsCacheProfilePrefObserver
|
2001-03-29 09:54:58 +04:00
|
|
|
*****************************************************************************/
|
2001-06-28 05:30:26 +04:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark nsCacheProfilePrefObserver
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define DISK_CACHE_ENABLE_PREF "browser.cache.disk.enable"
|
2001-09-20 05:12:35 +04:00
|
|
|
#define DISK_CACHE_DIR_PREF "browser.cache.disk.parent_directory"
|
2001-06-28 05:30:26 +04:00
|
|
|
#define DISK_CACHE_CAPACITY_PREF "browser.cache.disk.capacity"
|
2003-04-23 09:15:48 +04:00
|
|
|
#define DISK_CACHE_MAX_ENTRY_SIZE_PREF "browser.cache.disk.max_entry_size"
|
2010-02-24 04:12:33 +03:00
|
|
|
#define DISK_CACHE_CAPACITY 51200
|
2001-06-28 05:30:26 +04:00
|
|
|
|
2007-03-14 04:52:07 +03:00
|
|
|
#define OFFLINE_CACHE_ENABLE_PREF "browser.cache.offline.enable"
|
|
|
|
#define OFFLINE_CACHE_DIR_PREF "browser.cache.offline.parent_directory"
|
|
|
|
#define OFFLINE_CACHE_CAPACITY_PREF "browser.cache.offline.capacity"
|
2007-05-15 00:09:20 +04:00
|
|
|
#define OFFLINE_CACHE_CAPACITY 512000
|
2007-03-14 04:52:07 +03:00
|
|
|
|
2001-06-28 05:30:26 +04:00
|
|
|
#define MEMORY_CACHE_ENABLE_PREF "browser.cache.memory.enable"
|
|
|
|
#define MEMORY_CACHE_CAPACITY_PREF "browser.cache.memory.capacity"
|
2003-04-23 09:15:48 +04:00
|
|
|
#define MEMORY_CACHE_MAX_ENTRY_SIZE_PREF "browser.cache.memory.max_entry_size"
|
2001-03-29 09:54:58 +04:00
|
|
|
|
2008-02-10 07:53:28 +03:00
|
|
|
static const char * observerList[] = {
|
|
|
|
"profile-before-change",
|
|
|
|
"profile-after-change",
|
2008-10-25 14:26:20 +04:00
|
|
|
NS_XPCOM_SHUTDOWN_OBSERVER_ID,
|
|
|
|
NS_PRIVATE_BROWSING_SWITCH_TOPIC
|
2008-02-10 07:53:28 +03:00
|
|
|
};
|
|
|
|
static const char * prefList[] = {
|
|
|
|
#ifdef NECKO_DISK_CACHE
|
|
|
|
DISK_CACHE_ENABLE_PREF,
|
|
|
|
DISK_CACHE_CAPACITY_PREF,
|
|
|
|
DISK_CACHE_DIR_PREF,
|
|
|
|
#endif
|
|
|
|
#ifdef NECKO_OFFLINE_CACHE
|
|
|
|
OFFLINE_CACHE_ENABLE_PREF,
|
|
|
|
OFFLINE_CACHE_CAPACITY_PREF,
|
|
|
|
OFFLINE_CACHE_DIR_PREF,
|
|
|
|
#endif
|
|
|
|
MEMORY_CACHE_ENABLE_PREF,
|
|
|
|
MEMORY_CACHE_CAPACITY_PREF
|
|
|
|
};
|
2001-03-29 09:54:58 +04:00
|
|
|
|
2001-06-28 05:30:26 +04:00
|
|
|
class nsCacheProfilePrefObserver : public nsIObserver
|
2001-03-29 09:54:58 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
2001-06-28 05:30:26 +04:00
|
|
|
nsCacheProfilePrefObserver()
|
|
|
|
: mHaveProfile(PR_FALSE)
|
|
|
|
, mDiskCacheEnabled(PR_FALSE)
|
|
|
|
, mDiskCacheCapacity(0)
|
2007-03-14 04:52:07 +03:00
|
|
|
, mOfflineCacheEnabled(PR_FALSE)
|
|
|
|
, mOfflineCacheCapacity(0)
|
2001-06-28 05:30:26 +04:00
|
|
|
, mMemoryCacheEnabled(PR_TRUE)
|
2003-04-23 09:15:48 +04:00
|
|
|
, mMemoryCacheCapacity(-1)
|
2008-10-25 14:26:20 +04:00
|
|
|
, mInPrivateBrowsing(PR_FALSE)
|
2001-03-29 09:54:58 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2001-06-28 05:30:26 +04:00
|
|
|
virtual ~nsCacheProfilePrefObserver() {}
|
2001-03-29 09:54:58 +04:00
|
|
|
|
2001-06-28 05:30:26 +04:00
|
|
|
nsresult Install();
|
2006-05-10 21:30:15 +04:00
|
|
|
void Remove();
|
2004-01-29 02:45:17 +03:00
|
|
|
nsresult ReadPrefs(nsIPrefBranch* branch);
|
2001-06-28 05:30:26 +04:00
|
|
|
|
|
|
|
PRBool DiskCacheEnabled();
|
|
|
|
PRInt32 DiskCacheCapacity() { return mDiskCacheCapacity; }
|
|
|
|
nsILocalFile * DiskCacheParentDirectory() { return mDiskCacheParentDirectory; }
|
2007-03-14 04:52:07 +03:00
|
|
|
|
|
|
|
PRBool OfflineCacheEnabled();
|
|
|
|
PRInt32 OfflineCacheCapacity() { return mOfflineCacheCapacity; }
|
|
|
|
nsILocalFile * OfflineCacheParentDirectory() { return mOfflineCacheParentDirectory; }
|
2001-06-28 05:30:26 +04:00
|
|
|
|
|
|
|
PRBool MemoryCacheEnabled();
|
2005-10-09 23:58:06 +04:00
|
|
|
PRInt32 MemoryCacheCapacity();
|
2001-03-29 09:54:58 +04:00
|
|
|
|
|
|
|
private:
|
2001-06-28 05:30:26 +04:00
|
|
|
PRBool mHaveProfile;
|
|
|
|
|
|
|
|
PRBool mDiskCacheEnabled;
|
2008-12-15 08:57:24 +03:00
|
|
|
PRInt32 mDiskCacheCapacity; // in kilobytes
|
2001-06-28 05:30:26 +04:00
|
|
|
nsCOMPtr<nsILocalFile> mDiskCacheParentDirectory;
|
2007-03-14 04:52:07 +03:00
|
|
|
|
|
|
|
PRBool mOfflineCacheEnabled;
|
2008-12-15 08:57:24 +03:00
|
|
|
PRInt32 mOfflineCacheCapacity; // in kilobytes
|
2007-03-14 04:52:07 +03:00
|
|
|
nsCOMPtr<nsILocalFile> mOfflineCacheParentDirectory;
|
2001-06-28 05:30:26 +04:00
|
|
|
|
|
|
|
PRBool mMemoryCacheEnabled;
|
2008-12-15 08:57:24 +03:00
|
|
|
PRInt32 mMemoryCacheCapacity; // in kilobytes
|
2008-10-25 14:26:20 +04:00
|
|
|
|
|
|
|
PRBool mInPrivateBrowsing;
|
2001-03-29 09:54:58 +04:00
|
|
|
};
|
|
|
|
|
2003-09-08 03:12:15 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(nsCacheProfilePrefObserver, nsIObserver)
|
2001-03-29 09:54:58 +04:00
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2001-06-28 05:30:26 +04:00
|
|
|
nsCacheProfilePrefObserver::Install()
|
2001-03-29 09:54:58 +04:00
|
|
|
{
|
2001-06-28 05:30:26 +04:00
|
|
|
// install profile-change observer
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (!observerService)
|
|
|
|
return NS_ERROR_FAILURE;
|
2001-06-28 05:30:26 +04:00
|
|
|
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
nsresult rv, rv2 = NS_OK;
|
2009-08-05 06:39:06 +04:00
|
|
|
for (unsigned int i=0; i<NS_ARRAY_LENGTH(observerList); i++) {
|
2008-02-10 07:53:28 +03:00
|
|
|
rv = observerService->AddObserver(this, observerList[i], PR_FALSE);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
rv2 = rv;
|
|
|
|
}
|
2001-06-28 05:30:26 +04:00
|
|
|
|
|
|
|
// install preferences observer
|
2005-02-25 23:46:35 +03:00
|
|
|
nsCOMPtr<nsIPrefBranch2> branch = do_GetService(NS_PREFSERVICE_CONTRACTID);
|
2003-04-23 09:15:48 +04:00
|
|
|
if (!branch) return NS_ERROR_FAILURE;
|
|
|
|
|
2009-08-05 06:39:06 +04:00
|
|
|
for (unsigned int i=0; i<NS_ARRAY_LENGTH(prefList); i++) {
|
2003-04-23 09:15:48 +04:00
|
|
|
rv = branch->AddObserver(prefList[i], this, PR_FALSE);
|
2008-02-10 07:53:28 +03:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
rv2 = rv;
|
2003-04-23 09:15:48 +04:00
|
|
|
}
|
2001-04-26 22:41:11 +04:00
|
|
|
|
2008-10-25 14:26:20 +04:00
|
|
|
// determine the initial status of the private browsing mode
|
|
|
|
nsCOMPtr<nsIPrivateBrowsingService> pbs =
|
|
|
|
do_GetService(NS_PRIVATE_BROWSING_SERVICE_CONTRACTID);
|
|
|
|
if (pbs)
|
|
|
|
pbs->GetPrivateBrowsingEnabled(&mInPrivateBrowsing);
|
|
|
|
|
2003-04-23 09:15:48 +04:00
|
|
|
// Determine if we have a profile already
|
|
|
|
// Install() is called *after* the profile-after-change notification
|
|
|
|
// when there is only a single profile, or it is specified on the
|
|
|
|
// commandline at startup.
|
|
|
|
// In that case, we detect the presence of a profile by the existence
|
|
|
|
// of the NS_APP_USER_PROFILE_50_DIR directory.
|
2001-03-29 09:54:58 +04:00
|
|
|
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
nsCOMPtr<nsIFile> directory;
|
2003-04-23 09:15:48 +04:00
|
|
|
rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
|
|
|
|
getter_AddRefs(directory));
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2002-10-09 02:35:08 +04:00
|
|
|
mHaveProfile = PR_TRUE;
|
|
|
|
|
2004-01-29 02:45:17 +03:00
|
|
|
rv = ReadPrefs(branch);
|
2008-02-10 07:53:28 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return rv2;
|
2001-03-29 09:54:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
void
|
2001-06-28 05:30:26 +04:00
|
|
|
nsCacheProfilePrefObserver::Remove()
|
2001-03-29 09:54:58 +04:00
|
|
|
{
|
2001-06-28 05:30:26 +04:00
|
|
|
// remove Observer Service observers
|
2006-05-10 21:30:15 +04:00
|
|
|
nsCOMPtr<nsIObserverService> obs =
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
mozilla::services::GetObserverService();
|
2006-05-10 21:30:15 +04:00
|
|
|
if (obs) {
|
2009-08-05 06:39:06 +04:00
|
|
|
for (unsigned int i=0; i<NS_ARRAY_LENGTH(observerList); i++) {
|
2008-02-10 07:53:28 +03:00
|
|
|
obs->RemoveObserver(this, observerList[i]);
|
|
|
|
}
|
2006-05-10 21:30:15 +04:00
|
|
|
}
|
2001-06-28 05:30:26 +04:00
|
|
|
|
|
|
|
// remove Pref Service observers
|
2006-05-10 21:30:15 +04:00
|
|
|
nsCOMPtr<nsIPrefBranch2> prefs =
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
do_GetService(NS_PREFSERVICE_CONTRACTID);
|
|
|
|
if (!prefs)
|
|
|
|
return;
|
|
|
|
for (unsigned int i=0; i<NS_ARRAY_LENGTH(prefList); i++)
|
|
|
|
prefs->RemoveObserver(prefList[i], this); // remove cache pref observers
|
2001-03-29 09:54:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-06-28 05:30:26 +04:00
|
|
|
nsCacheProfilePrefObserver::Observe(nsISupports * subject,
|
2003-04-23 09:15:48 +04:00
|
|
|
const char * topic,
|
|
|
|
const PRUnichar * data_unicode)
|
2001-03-29 09:54:58 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_ConvertUTF16toUTF8 data(data_unicode);
|
2003-04-23 09:15:48 +04:00
|
|
|
CACHE_LOG_ALWAYS(("Observe [topic=%s data=%s]\n", topic, data.get()));
|
|
|
|
|
|
|
|
if (!strcmp(NS_XPCOM_SHUTDOWN_OBSERVER_ID, topic)) {
|
2001-06-28 05:30:26 +04:00
|
|
|
// xpcom going away, shutdown cache service
|
|
|
|
if (nsCacheService::GlobalInstance())
|
|
|
|
nsCacheService::GlobalInstance()->Shutdown();
|
|
|
|
|
2003-04-23 09:15:48 +04:00
|
|
|
} else if (!strcmp("profile-before-change", topic)) {
|
2001-06-28 05:30:26 +04:00
|
|
|
// profile before change
|
|
|
|
mHaveProfile = PR_FALSE;
|
2001-08-17 04:01:01 +04:00
|
|
|
|
2001-06-28 05:30:26 +04:00
|
|
|
// XXX shutdown devices
|
2003-04-23 09:15:48 +04:00
|
|
|
nsCacheService::OnProfileShutdown(!strcmp("shutdown-cleanse",
|
|
|
|
data.get()));
|
2001-06-28 05:30:26 +04:00
|
|
|
|
2003-04-23 09:15:48 +04:00
|
|
|
} else if (!strcmp("profile-after-change", topic)) {
|
2001-06-28 05:30:26 +04:00
|
|
|
// profile after change
|
|
|
|
mHaveProfile = PR_TRUE;
|
2004-01-29 02:45:17 +03:00
|
|
|
nsCOMPtr<nsIPrefBranch> branch = do_GetService(NS_PREFSERVICE_CONTRACTID);
|
|
|
|
ReadPrefs(branch);
|
2001-08-17 04:01:01 +04:00
|
|
|
nsCacheService::OnProfileChanged();
|
2001-06-28 05:30:26 +04:00
|
|
|
|
2003-04-23 09:15:48 +04:00
|
|
|
} else if (!strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, topic)) {
|
|
|
|
|
|
|
|
// ignore pref changes until we're done switch profiles
|
2001-06-28 05:30:26 +04:00
|
|
|
if (!mHaveProfile) return NS_OK;
|
2003-04-23 09:15:48 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPrefBranch> branch = do_QueryInterface(subject, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-03-29 09:54:58 +04:00
|
|
|
|
2003-04-23 07:47:43 +04:00
|
|
|
#ifdef NECKO_DISK_CACHE
|
2001-03-29 09:54:58 +04:00
|
|
|
// which preference changed?
|
2003-04-23 09:15:48 +04:00
|
|
|
if (!strcmp(DISK_CACHE_ENABLE_PREF, data.get())) {
|
2001-03-29 09:54:58 +04:00
|
|
|
|
2008-10-25 14:26:20 +04:00
|
|
|
if (!mInPrivateBrowsing) {
|
|
|
|
rv = branch->GetBoolPref(DISK_CACHE_ENABLE_PREF,
|
|
|
|
&mDiskCacheEnabled);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
nsCacheService::SetDiskCacheEnabled(DiskCacheEnabled());
|
|
|
|
}
|
2001-03-29 09:54:58 +04:00
|
|
|
|
2003-04-23 09:15:48 +04:00
|
|
|
} else if (!strcmp(DISK_CACHE_CAPACITY_PREF, data.get())) {
|
2001-06-28 05:30:26 +04:00
|
|
|
|
|
|
|
PRInt32 capacity = 0;
|
2003-04-23 09:15:48 +04:00
|
|
|
rv = branch->GetIntPref(DISK_CACHE_CAPACITY_PREF, &capacity);
|
2001-06-28 05:30:26 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-08-07 05:13:29 +04:00
|
|
|
mDiskCacheCapacity = PR_MAX(0, capacity);
|
2001-06-28 05:30:26 +04:00
|
|
|
nsCacheService::SetDiskCacheCapacity(mDiskCacheCapacity);
|
|
|
|
#if 0
|
2003-04-23 09:15:48 +04:00
|
|
|
} else if (!strcmp(DISK_CACHE_DIR_PREF, data.get())) {
|
|
|
|
// XXX We probaby don't want to respond to this pref except after
|
|
|
|
// XXX profile changes. Ideally, there should be somekind of user
|
|
|
|
// XXX notification that the pref change won't take effect until
|
|
|
|
// XXX the next time the profile changes (browser launch)
|
2001-06-28 05:30:26 +04:00
|
|
|
#endif
|
2003-04-23 09:15:48 +04:00
|
|
|
} else
|
2008-02-10 07:53:28 +03:00
|
|
|
#endif // !NECKO_DISK_CACHE
|
2007-03-14 04:52:07 +03:00
|
|
|
|
2008-02-10 07:53:28 +03:00
|
|
|
#ifdef NECKO_OFFLINE_CACHE
|
2007-03-14 04:52:07 +03:00
|
|
|
// which preference changed?
|
|
|
|
if (!strcmp(OFFLINE_CACHE_ENABLE_PREF, data.get())) {
|
|
|
|
|
2008-10-25 14:26:20 +04:00
|
|
|
if (!mInPrivateBrowsing) {
|
|
|
|
rv = branch->GetBoolPref(OFFLINE_CACHE_ENABLE_PREF,
|
|
|
|
&mOfflineCacheEnabled);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
nsCacheService::SetOfflineCacheEnabled(OfflineCacheEnabled());
|
|
|
|
}
|
2007-03-14 04:52:07 +03:00
|
|
|
|
|
|
|
} else if (!strcmp(OFFLINE_CACHE_CAPACITY_PREF, data.get())) {
|
|
|
|
|
|
|
|
PRInt32 capacity = 0;
|
|
|
|
rv = branch->GetIntPref(OFFLINE_CACHE_CAPACITY_PREF, &capacity);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
mOfflineCacheCapacity = PR_MAX(0, capacity);
|
|
|
|
nsCacheService::SetOfflineCacheCapacity(mOfflineCacheCapacity);
|
|
|
|
#if 0
|
2008-02-10 07:53:28 +03:00
|
|
|
} else if (!strcmp(OFFLINE_CACHE_DIR_PREF, data.get())) {
|
2007-03-14 04:52:07 +03:00
|
|
|
// XXX We probaby don't want to respond to this pref except after
|
|
|
|
// XXX profile changes. Ideally, there should be some kind of user
|
|
|
|
// XXX notification that the pref change won't take effect until
|
|
|
|
// XXX the next time the profile changes (browser launch)
|
|
|
|
#endif
|
|
|
|
} else
|
2008-02-10 07:53:28 +03:00
|
|
|
#endif // !NECKO_OFFLINE_CACHE
|
2007-03-14 04:52:07 +03:00
|
|
|
|
2003-04-23 09:15:48 +04:00
|
|
|
if (!strcmp(MEMORY_CACHE_ENABLE_PREF, data.get())) {
|
2003-04-23 07:47:43 +04:00
|
|
|
|
2003-04-23 09:15:48 +04:00
|
|
|
rv = branch->GetBoolPref(MEMORY_CACHE_ENABLE_PREF,
|
|
|
|
&mMemoryCacheEnabled);
|
2001-06-28 05:30:26 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2005-10-09 23:58:06 +04:00
|
|
|
nsCacheService::SetMemoryCache();
|
2001-06-28 05:30:26 +04:00
|
|
|
|
2003-04-23 09:15:48 +04:00
|
|
|
} else if (!strcmp(MEMORY_CACHE_CAPACITY_PREF, data.get())) {
|
2001-03-29 09:54:58 +04:00
|
|
|
|
2005-10-12 06:18:32 +04:00
|
|
|
mMemoryCacheCapacity = -1;
|
2003-04-23 09:15:48 +04:00
|
|
|
(void) branch->GetIntPref(MEMORY_CACHE_CAPACITY_PREF,
|
|
|
|
&mMemoryCacheCapacity);
|
2005-10-09 23:58:06 +04:00
|
|
|
nsCacheService::SetMemoryCache();
|
2001-03-29 09:54:58 +04:00
|
|
|
}
|
2008-10-25 14:26:20 +04:00
|
|
|
} else if (!strcmp(NS_PRIVATE_BROWSING_SWITCH_TOPIC, topic)) {
|
|
|
|
if (!strcmp(NS_PRIVATE_BROWSING_ENTER, data.get())) {
|
|
|
|
mInPrivateBrowsing = PR_TRUE;
|
|
|
|
|
|
|
|
nsCacheService::OnEnterExitPrivateBrowsing();
|
|
|
|
|
|
|
|
#ifdef NECKO_DISK_CACHE
|
|
|
|
mDiskCacheEnabled = PR_FALSE;
|
|
|
|
nsCacheService::SetDiskCacheEnabled(DiskCacheEnabled());
|
|
|
|
#endif // !NECKO_DISK_CACHE
|
|
|
|
|
|
|
|
#ifdef NECKO_OFFLINE_CACHE
|
|
|
|
mOfflineCacheEnabled = PR_FALSE;
|
|
|
|
nsCacheService::SetOfflineCacheEnabled(OfflineCacheEnabled());
|
|
|
|
#endif // !NECKO_OFFLINE_CACHE
|
|
|
|
} else if (!strcmp(NS_PRIVATE_BROWSING_LEAVE, data.get())) {
|
|
|
|
mInPrivateBrowsing = PR_FALSE;
|
|
|
|
|
|
|
|
nsCacheService::OnEnterExitPrivateBrowsing();
|
|
|
|
|
|
|
|
#if defined(NECKO_DISK_CACHE) || defined(NECKO_OFFLINE_CACHE)
|
|
|
|
nsCOMPtr<nsIPrefBranch> branch = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
#endif // !NECKO_DISK_CACHE && !NECKO_OFFLINE_CACHE
|
|
|
|
|
|
|
|
#ifdef NECKO_DISK_CACHE
|
|
|
|
mDiskCacheEnabled = PR_TRUE; // by default enabled
|
|
|
|
(void) branch->GetBoolPref(DISK_CACHE_ENABLE_PREF,
|
|
|
|
&mDiskCacheEnabled);
|
|
|
|
nsCacheService::SetDiskCacheEnabled(DiskCacheEnabled());
|
|
|
|
#endif // !NECKO_DISK_CACHE
|
|
|
|
|
|
|
|
#ifdef NECKO_OFFLINE_CACHE
|
|
|
|
mOfflineCacheEnabled = PR_TRUE; // by default enabled
|
|
|
|
(void) branch->GetBoolPref(OFFLINE_CACHE_ENABLE_PREF,
|
|
|
|
&mOfflineCacheEnabled);
|
|
|
|
nsCacheService::SetOfflineCacheEnabled(OfflineCacheEnabled());
|
|
|
|
#endif // !NECKO_OFFLINE_CACHE
|
|
|
|
}
|
2001-03-29 09:54:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-28 05:30:26 +04:00
|
|
|
nsresult
|
2004-01-29 02:45:17 +03:00
|
|
|
nsCacheProfilePrefObserver::ReadPrefs(nsIPrefBranch* branch)
|
2001-06-28 05:30:26 +04:00
|
|
|
{
|
2003-04-23 09:15:48 +04:00
|
|
|
nsresult rv = NS_OK;
|
2001-06-28 05:30:26 +04:00
|
|
|
|
2003-04-23 07:47:43 +04:00
|
|
|
#ifdef NECKO_DISK_CACHE
|
2001-06-28 05:30:26 +04:00
|
|
|
// read disk cache device prefs
|
2008-10-25 14:26:20 +04:00
|
|
|
if (!mInPrivateBrowsing) {
|
|
|
|
mDiskCacheEnabled = PR_TRUE; // presume disk cache is enabled
|
|
|
|
(void) branch->GetBoolPref(DISK_CACHE_ENABLE_PREF, &mDiskCacheEnabled);
|
|
|
|
}
|
2001-06-28 05:30:26 +04:00
|
|
|
|
2003-04-23 09:15:48 +04:00
|
|
|
mDiskCacheCapacity = DISK_CACHE_CAPACITY;
|
|
|
|
(void)branch->GetIntPref(DISK_CACHE_CAPACITY_PREF, &mDiskCacheCapacity);
|
|
|
|
mDiskCacheCapacity = PR_MAX(0, mDiskCacheCapacity);
|
2001-06-28 05:30:26 +04:00
|
|
|
|
2003-04-23 09:15:48 +04:00
|
|
|
(void) branch->GetComplexValue(DISK_CACHE_DIR_PREF, // ignore error
|
|
|
|
NS_GET_IID(nsILocalFile),
|
|
|
|
getter_AddRefs(mDiskCacheParentDirectory));
|
2001-06-28 05:30:26 +04:00
|
|
|
|
|
|
|
if (!mDiskCacheParentDirectory) {
|
|
|
|
nsCOMPtr<nsIFile> directory;
|
2003-04-23 09:15:48 +04:00
|
|
|
|
|
|
|
// try to get the disk cache parent directory
|
|
|
|
rv = NS_GetSpecialDirectory(NS_APP_CACHE_PARENT_DIR,
|
|
|
|
getter_AddRefs(directory));
|
|
|
|
if (NS_FAILED(rv)) {
|
2003-04-23 10:37:36 +04:00
|
|
|
// try to get the profile directory (there may not be a profile yet)
|
2005-04-26 04:37:23 +04:00
|
|
|
nsCOMPtr<nsIFile> profDir;
|
|
|
|
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
|
|
|
|
getter_AddRefs(profDir));
|
|
|
|
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_LOCAL_50_DIR,
|
|
|
|
getter_AddRefs(directory));
|
|
|
|
if (!directory)
|
|
|
|
directory = profDir;
|
|
|
|
else if (profDir) {
|
|
|
|
PRBool same;
|
|
|
|
if (NS_SUCCEEDED(profDir->Equals(directory, &same)) && !same) {
|
|
|
|
// We no longer store the cache directory in the main
|
|
|
|
// profile directory, so we should cleanup the old one.
|
|
|
|
rv = profDir->AppendNative(NS_LITERAL_CSTRING("Cache"));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
PRBool exists;
|
|
|
|
if (NS_SUCCEEDED(profDir->Exists(&exists)) && exists)
|
2006-01-24 17:47:06 +03:00
|
|
|
DeleteDir(profDir, PR_FALSE, PR_FALSE);
|
2005-04-26 04:37:23 +04:00
|
|
|
}
|
|
|
|
}
|
2004-10-20 09:00:23 +04:00
|
|
|
}
|
2003-04-23 10:37:36 +04:00
|
|
|
}
|
2009-08-05 06:39:06 +04:00
|
|
|
// use file cache in build tree only if asked, to avoid cache dir litter
|
|
|
|
if (!directory && PR_GetEnv("NECKO_DEV_ENABLE_DISK_CACHE")) {
|
2005-04-26 04:37:23 +04:00
|
|
|
rv = NS_GetSpecialDirectory(NS_XPCOM_CURRENT_PROCESS_DIR,
|
|
|
|
getter_AddRefs(directory));
|
|
|
|
}
|
2001-06-28 05:30:26 +04:00
|
|
|
if (directory)
|
|
|
|
mDiskCacheParentDirectory = do_QueryInterface(directory, &rv);
|
|
|
|
}
|
2008-02-10 07:53:28 +03:00
|
|
|
#endif // !NECKO_DISK_CACHE
|
2007-03-14 04:52:07 +03:00
|
|
|
|
2008-02-10 07:53:28 +03:00
|
|
|
#ifdef NECKO_OFFLINE_CACHE
|
2007-03-14 04:52:07 +03:00
|
|
|
// read offline cache device prefs
|
2008-10-25 14:26:20 +04:00
|
|
|
if (!mInPrivateBrowsing) {
|
|
|
|
mOfflineCacheEnabled = PR_TRUE; // presume offline cache is enabled
|
|
|
|
(void) branch->GetBoolPref(OFFLINE_CACHE_ENABLE_PREF,
|
|
|
|
&mOfflineCacheEnabled);
|
|
|
|
}
|
2007-03-14 04:52:07 +03:00
|
|
|
|
|
|
|
mOfflineCacheCapacity = OFFLINE_CACHE_CAPACITY;
|
|
|
|
(void)branch->GetIntPref(OFFLINE_CACHE_CAPACITY_PREF,
|
|
|
|
&mOfflineCacheCapacity);
|
|
|
|
mOfflineCacheCapacity = PR_MAX(0, mOfflineCacheCapacity);
|
|
|
|
|
|
|
|
(void) branch->GetComplexValue(OFFLINE_CACHE_DIR_PREF, // ignore error
|
|
|
|
NS_GET_IID(nsILocalFile),
|
|
|
|
getter_AddRefs(mOfflineCacheParentDirectory));
|
|
|
|
|
|
|
|
if (!mOfflineCacheParentDirectory) {
|
|
|
|
nsCOMPtr<nsIFile> directory;
|
|
|
|
|
|
|
|
// try to get the offline cache parent directory
|
|
|
|
rv = NS_GetSpecialDirectory(NS_APP_CACHE_PARENT_DIR,
|
|
|
|
getter_AddRefs(directory));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// try to get the profile directory (there may not be a profile yet)
|
|
|
|
nsCOMPtr<nsIFile> profDir;
|
|
|
|
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
|
|
|
|
getter_AddRefs(profDir));
|
|
|
|
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_LOCAL_50_DIR,
|
|
|
|
getter_AddRefs(directory));
|
|
|
|
if (!directory)
|
|
|
|
directory = profDir;
|
|
|
|
}
|
|
|
|
#if DEBUG
|
|
|
|
if (!directory) {
|
|
|
|
// use current process directory during development
|
|
|
|
rv = NS_GetSpecialDirectory(NS_XPCOM_CURRENT_PROCESS_DIR,
|
|
|
|
getter_AddRefs(directory));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (directory)
|
|
|
|
mOfflineCacheParentDirectory = do_QueryInterface(directory, &rv);
|
|
|
|
}
|
2008-02-10 07:53:28 +03:00
|
|
|
#endif // !NECKO_OFFLINE_CACHE
|
2001-08-17 04:01:01 +04:00
|
|
|
|
2001-06-28 05:30:26 +04:00
|
|
|
// read memory cache device prefs
|
2003-04-23 09:15:48 +04:00
|
|
|
(void) branch->GetBoolPref(MEMORY_CACHE_ENABLE_PREF, &mMemoryCacheEnabled);
|
|
|
|
|
2005-10-12 06:18:32 +04:00
|
|
|
mMemoryCacheCapacity = -1;
|
2003-04-23 09:15:48 +04:00
|
|
|
(void) branch->GetIntPref(MEMORY_CACHE_CAPACITY_PREF,
|
|
|
|
&mMemoryCacheCapacity);
|
2001-06-28 05:30:26 +04:00
|
|
|
|
2003-04-23 09:15:48 +04:00
|
|
|
return rv;
|
2001-06-28 05:30:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsCacheProfilePrefObserver::DiskCacheEnabled()
|
|
|
|
{
|
|
|
|
if ((mDiskCacheCapacity == 0) || (!mDiskCacheParentDirectory)) return PR_FALSE;
|
|
|
|
return mDiskCacheEnabled;
|
|
|
|
}
|
|
|
|
|
2007-03-14 04:52:07 +03:00
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsCacheProfilePrefObserver::OfflineCacheEnabled()
|
|
|
|
{
|
|
|
|
if ((mOfflineCacheCapacity == 0) || (!mOfflineCacheParentDirectory))
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
return mOfflineCacheEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-28 05:30:26 +04:00
|
|
|
PRBool
|
|
|
|
nsCacheProfilePrefObserver::MemoryCacheEnabled()
|
|
|
|
{
|
|
|
|
if (mMemoryCacheCapacity == 0) return PR_FALSE;
|
|
|
|
return mMemoryCacheEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-09 23:58:06 +04:00
|
|
|
/**
|
|
|
|
* MemoryCacheCapacity
|
|
|
|
*
|
|
|
|
* If the browser.cache.memory.capacity preference is positive, we use that
|
|
|
|
* value for the amount of memory available for the cache.
|
|
|
|
*
|
|
|
|
* If browser.cache.memory.capacity is zero, the memory cache is disabled.
|
|
|
|
*
|
|
|
|
* If browser.cache.memory.capacity is negative or not present, we use a
|
2006-02-27 23:13:42 +03:00
|
|
|
* formula that grows less than linearly with the amount of system memory,
|
|
|
|
* with an upper limit on the cache size. No matter how much physical RAM is
|
|
|
|
* present, the default cache size would not exceed 32 MB. This maximum would
|
|
|
|
* apply only to systems with more than 4 GB of RAM (e.g. terminal servers)
|
2005-10-09 23:58:06 +04:00
|
|
|
*
|
|
|
|
* RAM Cache
|
|
|
|
* --- -----
|
|
|
|
* 32 Mb 2 Mb
|
|
|
|
* 64 Mb 4 Mb
|
2006-02-27 23:13:42 +03:00
|
|
|
* 128 Mb 6 Mb
|
|
|
|
* 256 Mb 10 Mb
|
|
|
|
* 512 Mb 14 Mb
|
|
|
|
* 1024 Mb 18 Mb
|
|
|
|
* 2048 Mb 24 Mb
|
|
|
|
* 4096 Mb 30 Mb
|
2005-10-09 23:58:06 +04:00
|
|
|
*
|
|
|
|
* The equation for this is (for cache size C and memory size K (kbytes)):
|
|
|
|
* x = log2(K) - 14
|
2006-02-27 23:13:42 +03:00
|
|
|
* C = x^2/3 + x + 2/3 + 0.1 (0.1 for rounding)
|
|
|
|
* if (C > 32) C = 32
|
2005-10-09 23:58:06 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
PRInt32
|
|
|
|
nsCacheProfilePrefObserver::MemoryCacheCapacity()
|
|
|
|
{
|
|
|
|
PRInt32 capacity = mMemoryCacheCapacity;
|
2008-09-12 15:44:19 +04:00
|
|
|
if (capacity >= 0) {
|
|
|
|
CACHE_LOG_DEBUG(("Memory cache capacity forced to %d\n", capacity));
|
2005-10-09 23:58:06 +04:00
|
|
|
return capacity;
|
2008-09-12 15:44:19 +04:00
|
|
|
}
|
2005-10-09 23:58:06 +04:00
|
|
|
|
2010-05-13 22:11:24 +04:00
|
|
|
static PRUint64 bytes = PR_GetPhysicalMemorySize();
|
2008-09-12 15:44:19 +04:00
|
|
|
CACHE_LOG_DEBUG(("Physical Memory size is %llu\n", bytes));
|
2005-10-09 23:58:06 +04:00
|
|
|
|
2008-09-15 02:45:52 +04:00
|
|
|
// 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;
|
2005-10-09 23:58:06 +04:00
|
|
|
|
|
|
|
// Conversion from unsigned int64 to double doesn't work on all platforms.
|
|
|
|
// We need to truncate the value at LL_MAXINT to make sure we don't
|
|
|
|
// overflow.
|
|
|
|
if (LL_CMP(bytes, >, LL_MAXINT))
|
|
|
|
bytes = LL_MAXINT;
|
|
|
|
|
|
|
|
PRUint64 kbytes;
|
|
|
|
LL_SHR(kbytes, bytes, 10);
|
|
|
|
|
|
|
|
double kBytesD;
|
|
|
|
LL_L2D(kBytesD, (PRInt64) kbytes);
|
|
|
|
|
|
|
|
double x = log(kBytesD)/log(2.0) - 14;
|
|
|
|
if (x > 0) {
|
2006-02-27 23:13:42 +03:00
|
|
|
capacity = (PRInt32)(x * x / 3.0 + x + 2.0 / 3 + 0.1); // 0.1 for rounding
|
|
|
|
if (capacity > 32)
|
|
|
|
capacity = 32;
|
2005-10-09 23:58:06 +04:00
|
|
|
capacity *= 1024;
|
|
|
|
} else {
|
|
|
|
capacity = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return capacity;
|
|
|
|
}
|
2001-06-28 05:30:26 +04:00
|
|
|
|
2001-03-29 09:54:58 +04:00
|
|
|
/******************************************************************************
|
|
|
|
* nsCacheService
|
|
|
|
*****************************************************************************/
|
2001-06-28 05:30:26 +04:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark nsCacheService
|
|
|
|
#endif
|
2001-03-29 09:54:58 +04:00
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
nsCacheService * nsCacheService::gService = nsnull;
|
|
|
|
|
2003-09-08 03:12:15 +04:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsCacheService, nsICacheService)
|
2001-02-23 16:18:01 +03:00
|
|
|
|
|
|
|
nsCacheService::nsCacheService()
|
2006-06-28 03:13:48 +04:00
|
|
|
: mLock(nsnull),
|
2002-08-07 05:13:29 +04:00
|
|
|
mInitialized(PR_FALSE),
|
2001-03-29 09:54:58 +04:00
|
|
|
mEnableMemoryDevice(PR_TRUE),
|
|
|
|
mEnableDiskDevice(PR_TRUE),
|
2001-02-27 08:35:53 +03:00
|
|
|
mMemoryDevice(nsnull),
|
2001-03-02 04:51:41 +03:00
|
|
|
mDiskDevice(nsnull),
|
2007-03-14 04:52:07 +03:00
|
|
|
mOfflineDevice(nsnull),
|
2001-03-13 18:43:17 +03:00
|
|
|
mTotalEntries(0),
|
|
|
|
mCacheHits(0),
|
|
|
|
mCacheMisses(0),
|
|
|
|
mMaxKeyLength(0),
|
|
|
|
mMaxDataSize(0),
|
|
|
|
mMaxMetaSize(0),
|
2001-03-02 04:51:41 +03:00
|
|
|
mDeactivateFailures(0),
|
|
|
|
mDeactivatedUnboundEntries(0)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2002-08-07 05:13:29 +04:00
|
|
|
NS_ASSERTION(gService==nsnull, "multiple nsCacheService instances!");
|
|
|
|
gService = this;
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
// create list of cache devices
|
|
|
|
PR_INIT_CLIST(&mDoomedEntries);
|
|
|
|
|
|
|
|
// allocate service lock
|
2006-06-28 03:13:48 +04:00
|
|
|
mLock = PR_NewLock();
|
|
|
|
|
|
|
|
#if defined(DEBUG)
|
|
|
|
mLockedThread = nsnull;
|
|
|
|
#endif
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCacheService::~nsCacheService()
|
|
|
|
{
|
2002-08-07 05:13:29 +04:00
|
|
|
if (mInitialized) // Shutdown hasn't been called yet.
|
2001-02-23 16:18:01 +03:00
|
|
|
(void) Shutdown();
|
|
|
|
|
2006-06-28 03:13:48 +04:00
|
|
|
PR_DestroyLock(mLock);
|
2001-02-23 16:18:01 +03:00
|
|
|
gService = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-01 00:58:51 +03:00
|
|
|
nsresult
|
2001-02-23 16:18:01 +03:00
|
|
|
nsCacheService::Init()
|
|
|
|
{
|
2010-05-20 03:22:19 +04:00
|
|
|
NS_TIME_FUNCTION;
|
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
NS_ASSERTION(!mInitialized, "nsCacheService already initialized.");
|
|
|
|
if (mInitialized)
|
2001-02-23 16:18:01 +03:00
|
|
|
return NS_ERROR_ALREADY_INITIALIZED;
|
|
|
|
|
2006-06-28 03:13:48 +04:00
|
|
|
if (mLock == nsnull)
|
2001-02-23 16:18:01 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
CACHE_LOG_INIT();
|
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
// initialize hashtable for active cache entries
|
2010-07-30 07:16:37 +04:00
|
|
|
nsresult rv = mActiveEntries.Init();
|
2002-08-07 05:13:29 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-03-29 09:54:58 +04:00
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
// create profile/preference observer
|
2001-06-28 05:30:26 +04:00
|
|
|
mObserver = new nsCacheProfilePrefObserver();
|
|
|
|
if (!mObserver) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(mObserver);
|
|
|
|
|
|
|
|
mObserver->Install();
|
2007-03-14 04:52:07 +03:00
|
|
|
mEnableDiskDevice = mObserver->DiskCacheEnabled();
|
|
|
|
mEnableOfflineDevice = mObserver->OfflineCacheEnabled();
|
|
|
|
mEnableMemoryDevice = mObserver->MemoryCacheEnabled();
|
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
mInitialized = PR_TRUE;
|
2001-02-27 08:35:53 +03:00
|
|
|
return NS_OK;
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-01 00:58:51 +03:00
|
|
|
void
|
2001-02-23 16:18:01 +03:00
|
|
|
nsCacheService::Shutdown()
|
|
|
|
{
|
2006-06-28 03:13:48 +04:00
|
|
|
nsCacheServiceAutoLock lock;
|
2002-08-07 05:13:29 +04:00
|
|
|
NS_ASSERTION(mInitialized,
|
2001-02-23 16:18:01 +03:00
|
|
|
"can't shutdown nsCacheService unless it has been initialized.");
|
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
if (mInitialized) {
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
mInitialized = PR_FALSE;
|
2001-06-28 05:30:26 +04:00
|
|
|
|
|
|
|
mObserver->Remove();
|
|
|
|
NS_RELEASE(mObserver);
|
|
|
|
|
2001-03-13 18:43:17 +03:00
|
|
|
// Clear entries
|
|
|
|
ClearDoomList();
|
|
|
|
ClearActiveEntries();
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2001-02-27 08:35:53 +03:00
|
|
|
// deallocate memory and disk caches
|
|
|
|
delete mMemoryDevice;
|
|
|
|
mMemoryDevice = nsnull;
|
|
|
|
|
2003-04-23 07:47:43 +04:00
|
|
|
#ifdef NECKO_DISK_CACHE
|
2001-02-27 08:35:53 +03:00
|
|
|
delete mDiskDevice;
|
|
|
|
mDiskDevice = nsnull;
|
2007-05-15 00:09:20 +04:00
|
|
|
#endif // !NECKO_DISK_CACHE
|
2003-04-23 09:15:48 +04:00
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
#ifdef NECKO_OFFLINE_CACHE
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_IF_RELEASE(mOfflineDevice);
|
2007-05-15 00:09:20 +04:00
|
|
|
#endif // !NECKO_OFFLINE_CACHE
|
2007-03-14 04:52:07 +03:00
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
#if defined(NECKO_DISK_CACHE) && defined(PR_LOGGING)
|
2003-04-23 09:15:48 +04:00
|
|
|
LogCacheStatistics();
|
|
|
|
#endif
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-10 22:11:40 +04:00
|
|
|
nsresult
|
2001-02-23 16:18:01 +03:00
|
|
|
nsCacheService::Create(nsISupports* aOuter, const nsIID& aIID, void* *aResult)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (aOuter != nsnull)
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
|
2001-06-28 05:30:26 +04:00
|
|
|
nsCacheService * cacheService = new nsCacheService();
|
2001-02-23 16:18:01 +03:00
|
|
|
if (cacheService == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(cacheService);
|
|
|
|
rv = cacheService->Init();
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = cacheService->QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
NS_RELEASE(cacheService);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-02-26 17:55:58 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-02-26 18:53:31 +03:00
|
|
|
nsCacheService::CreateSession(const char * clientID,
|
|
|
|
nsCacheStoragePolicy storagePolicy,
|
|
|
|
PRBool streamBased,
|
|
|
|
nsICacheSession **result)
|
2001-02-24 04:02:37 +03:00
|
|
|
{
|
2001-02-26 17:55:58 +03:00
|
|
|
*result = nsnull;
|
|
|
|
|
2001-10-19 06:05:12 +04:00
|
|
|
if (this == nsnull) return NS_ERROR_NOT_AVAILABLE;
|
2001-03-29 09:54:58 +04:00
|
|
|
|
2001-02-26 17:55:58 +03:00
|
|
|
nsCacheSession * session = new nsCacheSession(clientID, storagePolicy, streamBased);
|
|
|
|
if (!session) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2001-02-27 08:35:53 +03:00
|
|
|
NS_ADDREF(*result = session);
|
2001-02-26 17:55:58 +03:00
|
|
|
|
2001-02-27 08:35:53 +03:00
|
|
|
return NS_OK;
|
2001-02-24 04:02:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-09 12:14:08 +04:00
|
|
|
nsresult
|
|
|
|
nsCacheService::EvictEntriesForSession(nsCacheSession * session)
|
|
|
|
{
|
2002-08-07 05:13:29 +04:00
|
|
|
NS_ASSERTION(gService, "nsCacheService::gService is null.");
|
|
|
|
return gService->EvictEntriesForClient(session->ClientID()->get(),
|
2001-04-09 12:14:08 +04:00
|
|
|
session->StoragePolicy());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsCacheService::EvictEntriesForClient(const char * clientID,
|
|
|
|
nsCacheStoragePolicy storagePolicy)
|
|
|
|
{
|
2004-11-01 00:58:51 +03:00
|
|
|
if (this == nsnull) return NS_ERROR_NOT_AVAILABLE; // XXX eh?
|
2005-09-30 00:24:27 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obsSvc =
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 20:59:13 +04:00
|
|
|
mozilla::services::GetObserverService();
|
2005-09-30 00:24:27 +04:00
|
|
|
if (obsSvc) {
|
|
|
|
// Proxy to the UI thread since the observer service isn't thredsafe.
|
|
|
|
// We use an async proxy, since this it's not important whether this
|
|
|
|
// notification happens before or after the actual eviction.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obsProxy;
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_GetProxyForObject(NS_PROXY_TO_MAIN_THREAD,
|
|
|
|
NS_GET_IID(nsIObserverService), obsSvc,
|
|
|
|
NS_PROXY_ASYNC, getter_AddRefs(obsProxy));
|
2005-09-30 00:24:27 +04:00
|
|
|
|
|
|
|
if (obsProxy) {
|
|
|
|
obsProxy->NotifyObservers(this,
|
|
|
|
NS_CACHESERVICE_EMPTYCACHE_TOPIC_ID,
|
|
|
|
nsnull);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-28 03:13:48 +04:00
|
|
|
nsCacheServiceAutoLock lock;
|
2010-05-24 16:25:59 +04:00
|
|
|
nsresult res = NS_OK;
|
2001-04-09 12:14:08 +04:00
|
|
|
|
2003-04-23 07:47:43 +04:00
|
|
|
#ifdef NECKO_DISK_CACHE
|
2001-04-09 12:14:08 +04:00
|
|
|
if (storagePolicy == nsICache::STORE_ANYWHERE ||
|
|
|
|
storagePolicy == nsICache::STORE_ON_DISK) {
|
|
|
|
|
|
|
|
if (mEnableDiskDevice) {
|
2010-05-24 16:25:59 +04:00
|
|
|
nsresult rv;
|
|
|
|
if (!mDiskDevice)
|
2001-04-09 12:14:08 +04:00
|
|
|
rv = CreateDiskDevice();
|
2010-05-24 16:25:59 +04:00
|
|
|
if (mDiskDevice)
|
|
|
|
rv = mDiskDevice->EvictEntries(clientID);
|
|
|
|
if (NS_FAILED(rv)) res = rv;
|
2001-04-09 12:14:08 +04:00
|
|
|
}
|
|
|
|
}
|
2007-05-15 00:09:20 +04:00
|
|
|
#endif // ! NECKO_DISK_CACHE
|
2007-03-14 04:52:07 +03:00
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
#ifdef NECKO_OFFLINE_CACHE
|
2008-01-23 07:06:36 +03:00
|
|
|
// Only clear the offline cache if it has been specifically asked for.
|
|
|
|
if (storagePolicy == nsICache::STORE_OFFLINE) {
|
2007-03-14 04:52:07 +03:00
|
|
|
if (mEnableOfflineDevice) {
|
2010-05-24 16:25:59 +04:00
|
|
|
nsresult rv;
|
|
|
|
if (!mOfflineDevice)
|
2007-03-14 04:52:07 +03:00
|
|
|
rv = CreateOfflineDevice();
|
2010-05-24 16:25:59 +04:00
|
|
|
if (mOfflineDevice)
|
|
|
|
rv = mOfflineDevice->EvictEntries(clientID);
|
|
|
|
if (NS_FAILED(rv)) res = rv;
|
2007-03-14 04:52:07 +03:00
|
|
|
}
|
|
|
|
}
|
2007-05-15 00:09:20 +04:00
|
|
|
#endif // ! NECKO_OFFLINE_CACHE
|
2001-04-09 12:14:08 +04:00
|
|
|
|
|
|
|
if (storagePolicy == nsICache::STORE_ANYWHERE ||
|
|
|
|
storagePolicy == nsICache::STORE_IN_MEMORY) {
|
|
|
|
|
2005-09-23 23:21:36 +04:00
|
|
|
// If there is no memory device, there is no need to evict it...
|
|
|
|
if (mMemoryDevice) {
|
2010-05-24 16:25:59 +04:00
|
|
|
nsresult rv;
|
2001-04-09 12:14:08 +04:00
|
|
|
rv = mMemoryDevice->EvictEntries(clientID);
|
2010-05-24 16:25:59 +04:00
|
|
|
if (NS_FAILED(rv)) res = rv;
|
2001-04-09 12:14:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-24 16:25:59 +04:00
|
|
|
return res;
|
2001-04-09 12:14:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-24 04:25:25 +03:00
|
|
|
nsresult
|
|
|
|
nsCacheService::IsStorageEnabledForPolicy(nsCacheStoragePolicy storagePolicy,
|
|
|
|
PRBool * result)
|
|
|
|
{
|
|
|
|
if (gService == nsnull) return NS_ERROR_NOT_AVAILABLE;
|
2006-06-28 03:13:48 +04:00
|
|
|
nsCacheServiceAutoLock lock;
|
2002-06-04 00:05:53 +04:00
|
|
|
|
|
|
|
*result = gService->IsStorageEnabledForPolicy_Locked(storagePolicy);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
|
2002-06-04 00:05:53 +04:00
|
|
|
PRBool
|
|
|
|
nsCacheService::IsStorageEnabledForPolicy_Locked(nsCacheStoragePolicy storagePolicy)
|
|
|
|
{
|
2002-08-07 05:13:29 +04:00
|
|
|
if (gService->mEnableMemoryDevice &&
|
2002-01-24 04:25:25 +03:00
|
|
|
(storagePolicy == nsICache::STORE_ANYWHERE ||
|
|
|
|
storagePolicy == nsICache::STORE_IN_MEMORY)) {
|
2002-06-04 00:05:53 +04:00
|
|
|
return PR_TRUE;
|
2002-01-24 04:25:25 +03:00
|
|
|
}
|
2002-08-07 05:13:29 +04:00
|
|
|
if (gService->mEnableDiskDevice &&
|
2002-06-04 00:05:53 +04:00
|
|
|
(storagePolicy == nsICache::STORE_ANYWHERE ||
|
|
|
|
storagePolicy == nsICache::STORE_ON_DISK ||
|
|
|
|
storagePolicy == nsICache::STORE_ON_DISK_AS_FILE)) {
|
|
|
|
return PR_TRUE;
|
2002-01-24 04:25:25 +03:00
|
|
|
}
|
2007-03-14 04:52:07 +03:00
|
|
|
if (gService->mEnableOfflineDevice &&
|
|
|
|
storagePolicy == nsICache::STORE_OFFLINE) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
2002-08-07 05:13:29 +04:00
|
|
|
|
2002-06-04 00:05:53 +04:00
|
|
|
return PR_FALSE;
|
2002-01-24 04:25:25 +03:00
|
|
|
}
|
|
|
|
|
2001-02-24 04:02:37 +03:00
|
|
|
NS_IMETHODIMP nsCacheService::VisitEntries(nsICacheVisitor *visitor)
|
|
|
|
{
|
2005-12-07 08:35:39 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(visitor);
|
|
|
|
|
2006-06-28 03:13:48 +04:00
|
|
|
nsCacheServiceAutoLock lock;
|
2001-03-29 09:54:58 +04:00
|
|
|
|
|
|
|
if (!(mEnableDiskDevice || mEnableMemoryDevice))
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
// XXX record the fact that a visitation is in progress,
|
|
|
|
// XXX i.e. keep list of visitors in progress.
|
2001-03-08 23:58:38 +03:00
|
|
|
|
2001-03-29 09:54:58 +04:00
|
|
|
nsresult rv = NS_OK;
|
2005-09-23 23:21:36 +04:00
|
|
|
// If there is no memory device, there are then also no entries to visit...
|
|
|
|
if (mMemoryDevice) {
|
2001-03-29 09:54:58 +04:00
|
|
|
rv = mMemoryDevice->Visit(visitor);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
2001-03-24 00:06:39 +03:00
|
|
|
|
2003-04-23 07:47:43 +04:00
|
|
|
#ifdef NECKO_DISK_CACHE
|
2001-03-29 09:54:58 +04:00
|
|
|
if (mEnableDiskDevice) {
|
|
|
|
if (!mDiskDevice) {
|
|
|
|
rv = CreateDiskDevice();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
rv = mDiskDevice->Visit(visitor);
|
2001-03-24 00:06:39 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
2007-05-15 00:09:20 +04:00
|
|
|
#endif // !NECKO_DISK_CACHE
|
2007-03-14 04:52:07 +03:00
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
#ifdef NECKO_OFFLINE_CACHE
|
2007-03-14 04:52:07 +03:00
|
|
|
if (mEnableOfflineDevice) {
|
|
|
|
if (!mOfflineDevice) {
|
|
|
|
rv = CreateOfflineDevice();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
rv = mOfflineDevice->Visit(visitor);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
2007-05-15 00:09:20 +04:00
|
|
|
#endif // !NECKO_OFFLINE_CACHE
|
2001-03-24 00:06:39 +03:00
|
|
|
|
2001-03-08 23:58:38 +03:00
|
|
|
// XXX notify any shutdown process that visitation is complete for THIS visitor.
|
2001-03-29 09:54:58 +04:00
|
|
|
// XXX keep queue of visitors
|
2001-03-08 23:58:38 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2001-02-24 04:02:37 +03:00
|
|
|
}
|
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2001-03-29 09:54:58 +04:00
|
|
|
NS_IMETHODIMP nsCacheService::EvictEntries(nsCacheStoragePolicy storagePolicy)
|
2001-03-24 00:06:39 +03:00
|
|
|
{
|
2001-04-09 12:14:08 +04:00
|
|
|
return EvictEntriesForClient(nsnull, storagePolicy);
|
2001-03-29 09:54:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal Methods
|
|
|
|
*/
|
|
|
|
nsresult
|
|
|
|
nsCacheService::CreateDiskDevice()
|
|
|
|
{
|
2003-04-23 07:47:43 +04:00
|
|
|
#ifdef NECKO_DISK_CACHE
|
2008-08-27 03:09:02 +04:00
|
|
|
if (!mInitialized) return NS_ERROR_NOT_AVAILABLE;
|
2001-03-29 09:54:58 +04:00
|
|
|
if (!mEnableDiskDevice) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
if (mDiskDevice) return NS_OK;
|
|
|
|
|
|
|
|
mDiskDevice = new nsDiskCacheDevice;
|
|
|
|
if (!mDiskDevice) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2001-06-28 05:30:26 +04:00
|
|
|
// set the preferences
|
|
|
|
mDiskDevice->SetCacheParentDirectory(mObserver->DiskCacheParentDirectory());
|
|
|
|
mDiskDevice->SetCapacity(mObserver->DiskCacheCapacity());
|
|
|
|
|
2001-03-29 09:54:58 +04:00
|
|
|
nsresult rv = mDiskDevice->Init();
|
|
|
|
if (NS_FAILED(rv)) {
|
2001-04-26 09:07:28 +04:00
|
|
|
#if DEBUG
|
|
|
|
printf("###\n");
|
|
|
|
printf("### mDiskDevice->Init() failed (0x%.8x)\n", rv);
|
|
|
|
printf("### - disabling disk cache for this session.\n");
|
|
|
|
printf("###\n");
|
|
|
|
#endif
|
|
|
|
mEnableDiskDevice = PR_FALSE;
|
2001-03-29 09:54:58 +04:00
|
|
|
delete mDiskDevice;
|
|
|
|
mDiskDevice = nsnull;
|
|
|
|
}
|
|
|
|
return rv;
|
2003-04-23 07:47:43 +04:00
|
|
|
#else // !NECKO_DISK_CACHE
|
|
|
|
NS_NOTREACHED("nsCacheService::CreateDiskDevice");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
#endif
|
2001-03-29 09:54:58 +04:00
|
|
|
}
|
|
|
|
|
2007-03-14 04:52:07 +03:00
|
|
|
nsresult
|
|
|
|
nsCacheService::CreateOfflineDevice()
|
|
|
|
{
|
2007-05-15 00:09:20 +04:00
|
|
|
#ifdef NECKO_OFFLINE_CACHE
|
2007-03-14 04:52:07 +03:00
|
|
|
CACHE_LOG_ALWAYS(("Creating offline device"));
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
if (!mInitialized) return NS_ERROR_NOT_AVAILABLE;
|
2007-03-14 04:52:07 +03:00
|
|
|
if (!mEnableOfflineDevice) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
if (mOfflineDevice) return NS_OK;
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
mOfflineDevice = new nsOfflineCacheDevice;
|
2007-03-14 04:52:07 +03:00
|
|
|
if (!mOfflineDevice) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ADDREF(mOfflineDevice);
|
|
|
|
|
2007-03-14 04:52:07 +03:00
|
|
|
// set the preferences
|
2007-05-15 00:09:20 +04:00
|
|
|
mOfflineDevice->SetCacheParentDirectory(
|
|
|
|
mObserver->OfflineCacheParentDirectory());
|
2007-03-14 04:52:07 +03:00
|
|
|
mOfflineDevice->SetCapacity(mObserver->OfflineCacheCapacity());
|
|
|
|
|
|
|
|
nsresult rv = mOfflineDevice->Init();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
CACHE_LOG_DEBUG(("mOfflineDevice->Init() failed (0x%.8x)\n", rv));
|
|
|
|
CACHE_LOG_DEBUG((" - disabling offline cache for this session.\n"));
|
|
|
|
|
|
|
|
mEnableOfflineDevice = PR_FALSE;
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_RELEASE(mOfflineDevice);
|
2007-03-14 04:52:07 +03:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
#else // !NECKO_DISK_CACHE
|
|
|
|
NS_NOTREACHED("nsCacheService::CreateOfflineDevice");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
#endif
|
|
|
|
}
|
2001-03-29 09:54:58 +04:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsCacheService::CreateMemoryDevice()
|
|
|
|
{
|
2008-08-27 03:09:02 +04:00
|
|
|
if (!mInitialized) return NS_ERROR_NOT_AVAILABLE;
|
2001-03-29 09:54:58 +04:00
|
|
|
if (!mEnableMemoryDevice) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
if (mMemoryDevice) return NS_OK;
|
|
|
|
|
|
|
|
mMemoryDevice = new nsMemoryCacheDevice;
|
|
|
|
if (!mMemoryDevice) return NS_ERROR_OUT_OF_MEMORY;
|
2001-06-28 05:30:26 +04:00
|
|
|
|
|
|
|
// set preference
|
2008-09-12 15:44:19 +04:00
|
|
|
PRInt32 capacity = mObserver->MemoryCacheCapacity();
|
|
|
|
CACHE_LOG_DEBUG(("Creating memory device with capacity %d\n", capacity));
|
|
|
|
mMemoryDevice->SetCapacity(capacity);
|
2001-03-29 09:54:58 +04:00
|
|
|
|
|
|
|
nsresult rv = mMemoryDevice->Init();
|
|
|
|
if (NS_FAILED(rv)) {
|
2002-08-07 05:13:29 +04:00
|
|
|
NS_WARNING("Initialization of Memory Cache failed.");
|
2001-03-29 09:54:58 +04:00
|
|
|
delete mMemoryDevice;
|
|
|
|
mMemoryDevice = nsnull;
|
|
|
|
}
|
2001-03-24 00:06:39 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
nsresult
|
2001-02-27 08:35:53 +03:00
|
|
|
nsCacheService::CreateRequest(nsCacheSession * session,
|
2005-01-13 06:25:28 +03:00
|
|
|
const nsACString & clientKey,
|
2001-02-27 08:35:53 +03:00
|
|
|
nsCacheAccessMode accessRequested,
|
2001-05-09 07:36:00 +04:00
|
|
|
PRBool blockingMode,
|
2001-02-27 08:35:53 +03:00
|
|
|
nsICacheListener * listener,
|
|
|
|
nsCacheRequest ** request)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2005-09-23 23:21:36 +04:00
|
|
|
NS_ASSERTION(request, "CreateRequest: request is null");
|
2001-02-26 17:55:58 +03:00
|
|
|
|
2001-03-22 05:25:36 +03:00
|
|
|
nsCString * key = new nsCString(*session->ClientID());
|
2001-02-27 08:35:53 +03:00
|
|
|
if (!key)
|
2001-02-23 16:18:01 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2004-12-04 13:19:29 +03:00
|
|
|
key->Append(':');
|
2001-03-22 05:25:36 +03:00
|
|
|
key->Append(clientKey);
|
2001-03-13 18:43:17 +03:00
|
|
|
|
|
|
|
if (mMaxKeyLength < key->Length()) mMaxKeyLength = key->Length();
|
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
// create request
|
2001-05-09 07:36:00 +04:00
|
|
|
*request = new nsCacheRequest(key, listener, accessRequested, blockingMode, session);
|
2001-02-27 08:35:53 +03:00
|
|
|
if (!*request) {
|
|
|
|
delete key;
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2001-03-05 10:17:58 +03:00
|
|
|
if (!listener) return NS_OK; // we're sync, we're done.
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
// get the request's thread
|
|
|
|
(*request)->mThread = do_GetCurrentThread();
|
2001-03-05 10:17:58 +03:00
|
|
|
|
2001-02-27 08:35:53 +03:00
|
|
|
return NS_OK;
|
2001-03-05 10:17:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-28 03:13:48 +04:00
|
|
|
class nsCacheListenerEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsCacheListenerEvent(nsICacheListener *listener,
|
|
|
|
nsICacheEntryDescriptor *descriptor,
|
|
|
|
nsCacheAccessMode accessGranted,
|
|
|
|
nsresult status)
|
|
|
|
: mListener(listener) // transfers reference
|
|
|
|
, mDescriptor(descriptor) // transfers reference (may be null)
|
|
|
|
, mAccessGranted(accessGranted)
|
|
|
|
, mStatus(status)
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
mListener->OnCacheEntryAvailable(mDescriptor, mAccessGranted, mStatus);
|
|
|
|
|
|
|
|
NS_RELEASE(mListener);
|
|
|
|
NS_IF_RELEASE(mDescriptor);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// We explicitly leak mListener or mDescriptor if Run is not called
|
|
|
|
// because otherwise we cannot guarantee that they are destroyed on
|
|
|
|
// the right thread.
|
|
|
|
|
|
|
|
nsICacheListener *mListener;
|
|
|
|
nsICacheEntryDescriptor *mDescriptor;
|
|
|
|
nsCacheAccessMode mAccessGranted;
|
|
|
|
nsresult mStatus;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-03-05 10:17:58 +03:00
|
|
|
nsresult
|
2001-03-05 13:45:33 +03:00
|
|
|
nsCacheService::NotifyListener(nsCacheRequest * request,
|
|
|
|
nsICacheEntryDescriptor * descriptor,
|
|
|
|
nsCacheAccessMode accessGranted,
|
2006-06-28 03:13:48 +04:00
|
|
|
nsresult status)
|
2001-03-05 10:17:58 +03:00
|
|
|
{
|
2001-06-06 06:30:07 +04:00
|
|
|
NS_ASSERTION(request->mThread, "no thread set in async request!");
|
2006-05-10 21:30:15 +04:00
|
|
|
|
2006-06-28 03:13:48 +04:00
|
|
|
// Swap ownership, and release listener on target thread...
|
|
|
|
nsICacheListener *listener = request->mListener;
|
|
|
|
request->mListener = nsnull;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> ev =
|
|
|
|
new nsCacheListenerEvent(listener, descriptor,
|
|
|
|
accessGranted, status);
|
|
|
|
if (!ev) {
|
|
|
|
// Better to leak listener and descriptor if we fail because we don't
|
|
|
|
// want to destroy them inside the cache service lock or on potentially
|
|
|
|
// the wrong thread.
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2001-03-05 10:17:58 +03:00
|
|
|
|
2006-06-28 03:13:48 +04:00
|
|
|
return request->mThread->Dispatch(ev, NS_DISPATCH_NORMAL);
|
2001-03-05 13:45:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2001-05-09 07:36:00 +04:00
|
|
|
nsCacheService::ProcessRequest(nsCacheRequest * request,
|
|
|
|
PRBool calledFromOpenCacheEntry,
|
2001-03-05 13:45:33 +03:00
|
|
|
nsICacheEntryDescriptor ** result)
|
|
|
|
{
|
2006-06-28 03:13:48 +04:00
|
|
|
// !!! must be called with mLock held !!!
|
2001-03-05 13:45:33 +03:00
|
|
|
nsresult rv;
|
|
|
|
nsCacheEntry * entry = nsnull;
|
|
|
|
nsCacheAccessMode accessGranted = nsICache::ACCESS_NONE;
|
|
|
|
if (result) *result = nsnull;
|
2001-03-05 10:17:58 +03:00
|
|
|
|
|
|
|
while(1) { // Activate entry loop
|
|
|
|
rv = ActivateEntry(request, &entry); // get the entry for this request
|
|
|
|
if (NS_FAILED(rv)) break;
|
|
|
|
|
|
|
|
while(1) { // Request Access loop
|
|
|
|
NS_ASSERTION(entry, "no entry in Request Access loop!");
|
2001-03-05 13:45:33 +03:00
|
|
|
// entry->RequestAccess queues request on entry
|
2001-03-05 10:17:58 +03:00
|
|
|
rv = entry->RequestAccess(request, &accessGranted);
|
|
|
|
if (rv != NS_ERROR_CACHE_WAIT_FOR_VALIDATION) break;
|
|
|
|
|
2001-03-05 13:45:33 +03:00
|
|
|
if (request->mListener) // async exits - validate, doom, or close will resume
|
2001-05-09 07:36:00 +04:00
|
|
|
return rv;
|
2001-03-05 10:17:58 +03:00
|
|
|
|
2001-05-09 07:36:00 +04:00
|
|
|
if (request->IsBlocking()) {
|
2006-06-28 03:13:48 +04:00
|
|
|
// XXX this is probably wrong...
|
|
|
|
Unlock();
|
2001-05-09 07:36:00 +04:00
|
|
|
rv = request->WaitForValidation();
|
2006-06-28 03:13:48 +04:00
|
|
|
Lock();
|
2001-05-09 07:36:00 +04:00
|
|
|
}
|
2001-03-05 13:45:33 +03:00
|
|
|
|
|
|
|
PR_REMOVE_AND_INIT_LINK(request);
|
2001-05-09 07:36:00 +04:00
|
|
|
if (NS_FAILED(rv)) break; // non-blocking mode returns WAIT_FOR_VALIDATION error
|
2001-03-05 10:17:58 +03:00
|
|
|
// okay, we're ready to process this request, request access again
|
|
|
|
}
|
2001-03-05 13:45:33 +03:00
|
|
|
if (rv != NS_ERROR_CACHE_ENTRY_DOOMED) break;
|
|
|
|
|
|
|
|
if (entry->IsNotInUse()) {
|
|
|
|
// this request was the last one keeping it around, so get rid of it
|
|
|
|
DeactivateEntry(entry);
|
|
|
|
}
|
|
|
|
// loop back around to look for another entry
|
2001-03-05 10:17:58 +03:00
|
|
|
}
|
2001-03-05 13:45:33 +03:00
|
|
|
|
2006-06-28 03:13:48 +04:00
|
|
|
nsICacheEntryDescriptor *descriptor = nsnull;
|
2001-03-06 06:26:13 +03:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2006-06-28 03:13:48 +04:00
|
|
|
rv = entry->CreateDescriptor(request, accessGranted, &descriptor);
|
2001-03-05 10:17:58 +03:00
|
|
|
|
2001-03-05 13:45:33 +03:00
|
|
|
if (request->mListener) { // Asynchronous
|
2001-05-09 07:36:00 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv) && calledFromOpenCacheEntry)
|
|
|
|
return rv; // skip notifying listener, just return rv to caller
|
|
|
|
|
2001-03-05 13:45:33 +03:00
|
|
|
// call listener to report error or descriptor
|
2001-03-08 08:37:00 +03:00
|
|
|
nsresult rv2 = NotifyListener(request, descriptor, accessGranted, rv);
|
|
|
|
if (NS_FAILED(rv2) && NS_SUCCEEDED(rv)) {
|
|
|
|
rv = rv2; // trigger delete request
|
|
|
|
}
|
2001-03-05 13:45:33 +03:00
|
|
|
} else { // Synchronous
|
2006-06-28 03:13:48 +04:00
|
|
|
*result = descriptor;
|
2001-03-05 10:17:58 +03:00
|
|
|
}
|
2001-03-05 13:45:33 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsCacheService::OpenCacheEntry(nsCacheSession * session,
|
2005-01-13 06:25:28 +03:00
|
|
|
const nsACString & key,
|
2002-08-07 05:13:29 +04:00
|
|
|
nsCacheAccessMode accessRequested,
|
|
|
|
PRBool blockingMode,
|
|
|
|
nsICacheListener * listener,
|
|
|
|
nsICacheEntryDescriptor ** result)
|
2001-03-05 13:45:33 +03:00
|
|
|
{
|
2008-09-12 15:44:19 +04:00
|
|
|
CACHE_LOG_DEBUG(("Opening entry for session %p, key %s, mode %d, blocking %d\n",
|
|
|
|
session, PromiseFlatCString(key).get(), accessRequested,
|
|
|
|
blockingMode));
|
2002-08-07 05:13:29 +04:00
|
|
|
NS_ASSERTION(gService, "nsCacheService::gService is null.");
|
2001-03-07 07:05:36 +03:00
|
|
|
if (result)
|
2001-03-05 13:45:33 +03:00
|
|
|
*result = nsnull;
|
|
|
|
|
2007-08-29 02:05:32 +04:00
|
|
|
if (!gService->mInitialized)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2001-03-05 13:45:33 +03:00
|
|
|
nsCacheRequest * request = nsnull;
|
|
|
|
|
2006-06-28 03:13:48 +04:00
|
|
|
nsCacheServiceAutoLock lock;
|
2002-08-07 05:13:29 +04:00
|
|
|
nsresult rv = gService->CreateRequest(session,
|
|
|
|
key,
|
|
|
|
accessRequested,
|
|
|
|
blockingMode,
|
|
|
|
listener,
|
|
|
|
&request);
|
2001-03-05 13:45:33 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2008-09-12 15:44:19 +04:00
|
|
|
CACHE_LOG_DEBUG(("Created request %p\n", request));
|
|
|
|
|
2010-07-30 07:16:37 +04:00
|
|
|
rv = gService->ProcessRequest(request, PR_TRUE, result);
|
2001-03-05 13:45:33 +03:00
|
|
|
|
2010-07-30 07:16:37 +04:00
|
|
|
// delete requests that have completed
|
|
|
|
if (!(listener && (rv == NS_ERROR_CACHE_WAIT_FOR_VALIDATION)))
|
|
|
|
delete request;
|
2001-03-05 10:17:58 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-02-27 08:35:53 +03:00
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
nsresult
|
|
|
|
nsCacheService::ActivateEntry(nsCacheRequest * request,
|
|
|
|
nsCacheEntry ** result)
|
|
|
|
{
|
2008-09-12 15:44:19 +04:00
|
|
|
CACHE_LOG_DEBUG(("Activate entry for request %p\n", request));
|
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
NS_ASSERTION(request != nsnull, "ActivateEntry called with no request");
|
|
|
|
if (result) *result = nsnull;
|
2001-02-27 08:35:53 +03:00
|
|
|
if ((!request) || (!result)) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2002-06-04 00:05:53 +04:00
|
|
|
// check if the request can be satisfied
|
|
|
|
if (!mEnableMemoryDevice && !request->IsStreamBased())
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
if (!IsStorageEnabledForPolicy_Locked(request->StoragePolicy()))
|
2001-09-20 05:12:35 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2001-02-23 16:18:01 +03:00
|
|
|
|
|
|
|
// search active entries (including those not bound to device)
|
|
|
|
nsCacheEntry *entry = mActiveEntries.GetEntry(request->mKey);
|
2008-09-12 15:44:19 +04:00
|
|
|
CACHE_LOG_DEBUG(("Active entry for request %p is %p\n", request, entry));
|
2001-02-23 16:18:01 +03:00
|
|
|
|
|
|
|
if (!entry) {
|
2001-03-03 05:27:49 +03:00
|
|
|
// search cache devices for entry
|
2006-12-01 07:41:30 +03:00
|
|
|
PRBool collision = PR_FALSE;
|
|
|
|
entry = SearchCacheDevices(request->mKey, request->StoragePolicy(), &collision);
|
2008-09-12 15:44:19 +04:00
|
|
|
CACHE_LOG_DEBUG(("Device search for request %p returned %p\n",
|
|
|
|
request, entry));
|
2006-12-01 07:41:30 +03:00
|
|
|
// When there is a hashkey collision just refuse to cache it...
|
|
|
|
if (collision) return NS_ERROR_CACHE_IN_USE;
|
|
|
|
|
2001-03-03 07:42:21 +03:00
|
|
|
if (entry) entry->MarkInitialized();
|
2001-03-03 05:27:49 +03:00
|
|
|
}
|
|
|
|
|
2001-03-21 01:42:03 +03:00
|
|
|
if (entry) {
|
|
|
|
++mCacheHits;
|
2001-03-29 09:54:58 +04:00
|
|
|
entry->Fetched();
|
2001-03-21 01:42:03 +03:00
|
|
|
} else {
|
|
|
|
++mCacheMisses;
|
|
|
|
}
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2001-03-04 03:11:30 +03:00
|
|
|
if (entry &&
|
2001-03-05 10:17:58 +03:00
|
|
|
((request->AccessRequested() == nsICache::ACCESS_WRITE) ||
|
2008-04-09 09:22:32 +04:00
|
|
|
((request->StoragePolicy() != nsICache::STORE_OFFLINE) &&
|
|
|
|
(entry->mExpirationTime <= SecondsFromPRTime(PR_Now()) &&
|
|
|
|
request->WillDoomEntriesIfExpired()))))
|
|
|
|
|
2001-03-04 03:11:30 +03:00
|
|
|
{
|
|
|
|
// this is FORCE-WRITE request or the entry has expired
|
2002-08-07 05:13:29 +04:00
|
|
|
rv = DoomEntry_Internal(entry);
|
2001-03-03 05:27:49 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2001-03-04 03:11:30 +03:00
|
|
|
// XXX what to do? Increment FailedDooms counter?
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
2001-03-03 05:27:49 +03:00
|
|
|
entry = nsnull;
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
2001-03-03 05:27:49 +03:00
|
|
|
if (!entry) {
|
2003-07-09 06:28:07 +04:00
|
|
|
if (! (request->AccessRequested() & nsICache::ACCESS_WRITE)) {
|
|
|
|
// this is a READ-ONLY request
|
|
|
|
rv = NS_ERROR_CACHE_KEY_NOT_FOUND;
|
|
|
|
goto error;
|
|
|
|
}
|
2001-07-19 03:37:17 +04:00
|
|
|
|
2001-03-03 05:27:49 +03:00
|
|
|
entry = new nsCacheEntry(request->mKey,
|
2001-03-05 10:17:58 +03:00
|
|
|
request->IsStreamBased(),
|
|
|
|
request->StoragePolicy());
|
2001-03-03 05:27:49 +03:00
|
|
|
if (!entry)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2001-03-29 09:54:58 +04:00
|
|
|
entry->Fetched();
|
2001-03-13 18:43:17 +03:00
|
|
|
++mTotalEntries;
|
|
|
|
|
2001-03-04 03:11:30 +03:00
|
|
|
// XXX we could perform an early bind in some cases based on storage policy
|
2001-03-03 05:27:49 +03:00
|
|
|
}
|
|
|
|
|
2001-03-08 04:02:27 +03:00
|
|
|
if (!entry->IsActive()) {
|
|
|
|
rv = mActiveEntries.AddEntry(entry);
|
|
|
|
if (NS_FAILED(rv)) goto error;
|
2008-09-12 15:44:19 +04:00
|
|
|
CACHE_LOG_DEBUG(("Added entry %p to mActiveEntries\n", entry));
|
2001-03-08 04:02:27 +03:00
|
|
|
entry->MarkActive(); // mark entry active, because it's now in mActiveEntries
|
|
|
|
}
|
2001-02-27 08:35:53 +03:00
|
|
|
*result = entry;
|
2001-02-28 07:10:43 +03:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
error:
|
|
|
|
*result = nsnull;
|
|
|
|
if (entry) {
|
2001-03-05 13:45:33 +03:00
|
|
|
delete entry;
|
2001-02-28 07:10:43 +03:00
|
|
|
}
|
2001-02-23 16:18:01 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-27 08:35:53 +03:00
|
|
|
nsCacheEntry *
|
2006-12-01 07:41:30 +03:00
|
|
|
nsCacheService::SearchCacheDevices(nsCString * key, nsCacheStoragePolicy policy, PRBool *collision)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2001-02-27 08:35:53 +03:00
|
|
|
nsCacheEntry * entry = nsnull;
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2008-09-12 15:44:19 +04:00
|
|
|
CACHE_LOG_DEBUG(("mMemoryDevice: 0x%p\n", mMemoryDevice));
|
|
|
|
|
2006-12-01 07:41:30 +03:00
|
|
|
*collision = PR_FALSE;
|
2001-02-27 08:35:53 +03:00
|
|
|
if ((policy == nsICache::STORE_ANYWHERE) || (policy == nsICache::STORE_IN_MEMORY)) {
|
2005-09-23 23:21:36 +04:00
|
|
|
// If there is no memory device, then there is nothing to search...
|
2008-09-12 15:44:19 +04:00
|
|
|
if (mMemoryDevice) {
|
2006-12-01 07:41:30 +03:00
|
|
|
entry = mMemoryDevice->FindEntry(key, collision);
|
2008-09-12 15:44:19 +04:00
|
|
|
CACHE_LOG_DEBUG(("Searching mMemoryDevice for key %s found: 0x%p, "
|
|
|
|
"collision: %d\n", key->get(), entry, collision));
|
|
|
|
}
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
2001-02-27 08:35:53 +03:00
|
|
|
if (!entry &&
|
|
|
|
((policy == nsICache::STORE_ANYWHERE) || (policy == nsICache::STORE_ON_DISK))) {
|
2001-03-24 00:06:39 +03:00
|
|
|
|
2003-04-23 07:47:43 +04:00
|
|
|
#ifdef NECKO_DISK_CACHE
|
2001-03-29 09:54:58 +04:00
|
|
|
if (mEnableDiskDevice) {
|
|
|
|
if (!mDiskDevice) {
|
|
|
|
nsresult rv = CreateDiskDevice();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2006-12-01 07:41:30 +03:00
|
|
|
entry = mDiskDevice->FindEntry(key, collision);
|
2001-03-24 00:06:39 +03:00
|
|
|
}
|
2003-04-23 07:47:43 +04:00
|
|
|
#endif // !NECKO_DISK_CACHE
|
2001-02-27 08:35:53 +03:00
|
|
|
}
|
|
|
|
|
2007-03-14 04:52:07 +03:00
|
|
|
if (!entry && (policy == nsICache::STORE_OFFLINE ||
|
|
|
|
(policy == nsICache::STORE_ANYWHERE &&
|
|
|
|
gIOService->IsOffline()))) {
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
#ifdef NECKO_OFFLINE_CACHE
|
2007-03-14 04:52:07 +03:00
|
|
|
if (mEnableOfflineDevice) {
|
|
|
|
if (!mOfflineDevice) {
|
|
|
|
nsresult rv = CreateOfflineDevice();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry = mOfflineDevice->FindEntry(key, collision);
|
|
|
|
}
|
2007-05-15 00:09:20 +04:00
|
|
|
#endif // !NECKO_OFFLINE_CACHE
|
2007-03-14 04:52:07 +03:00
|
|
|
}
|
|
|
|
|
2001-02-27 08:35:53 +03:00
|
|
|
return entry;
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-05 13:45:33 +03:00
|
|
|
nsCacheDevice *
|
|
|
|
nsCacheService::EnsureEntryHasDevice(nsCacheEntry * entry)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2001-03-05 13:45:33 +03:00
|
|
|
nsCacheDevice * device = entry->CacheDevice();
|
|
|
|
if (device) return device;
|
2001-02-28 07:10:43 +03:00
|
|
|
|
2003-04-23 07:47:43 +04:00
|
|
|
#ifdef NECKO_DISK_CACHE
|
2001-03-29 09:54:58 +04:00
|
|
|
if (entry->IsStreamData() && entry->IsAllowedOnDisk() && mEnableDiskDevice) {
|
2001-02-28 07:34:41 +03:00
|
|
|
// this is the default
|
2001-03-24 00:06:39 +03:00
|
|
|
if (!mDiskDevice) {
|
2005-09-23 23:21:36 +04:00
|
|
|
(void)CreateDiskDevice(); // ignore the error (check for mDiskDevice instead)
|
2001-03-24 00:06:39 +03:00
|
|
|
}
|
|
|
|
|
2001-04-26 09:07:28 +04:00
|
|
|
if (mDiskDevice) {
|
2002-08-07 05:13:29 +04:00
|
|
|
entry->MarkBinding(); // enter state of binding
|
2005-09-23 23:21:36 +04:00
|
|
|
nsresult rv = mDiskDevice->BindEntry(entry);
|
2002-08-07 05:13:29 +04:00
|
|
|
entry->ClearBinding(); // exit state of binding
|
2001-04-26 09:07:28 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
device = mDiskDevice;
|
|
|
|
}
|
|
|
|
}
|
2003-04-23 07:47:43 +04:00
|
|
|
#endif // !NECKO_DISK_CACHE
|
2001-04-26 09:07:28 +04:00
|
|
|
|
|
|
|
// if we can't use mDiskDevice, try mMemoryDevice
|
2001-11-30 02:04:58 +03:00
|
|
|
if (!device && mEnableMemoryDevice && entry->IsAllowedInMemory()) {
|
2005-09-23 23:21:36 +04:00
|
|
|
if (!mMemoryDevice) {
|
|
|
|
(void)CreateMemoryDevice(); // ignore the error (check for mMemoryDevice instead)
|
|
|
|
}
|
|
|
|
if (mMemoryDevice) {
|
|
|
|
entry->MarkBinding(); // enter state of binding
|
|
|
|
nsresult rv = mMemoryDevice->BindEntry(entry);
|
|
|
|
entry->ClearBinding(); // exit state of binding
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
device = mMemoryDevice;
|
|
|
|
}
|
2001-02-28 07:10:43 +03:00
|
|
|
}
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
#ifdef NECKO_OFFLINE_CACHE
|
2007-03-14 04:52:07 +03:00
|
|
|
if (!device && entry->IsStreamData() &&
|
|
|
|
entry->IsAllowedOffline() && mEnableOfflineDevice) {
|
|
|
|
if (!mOfflineDevice) {
|
|
|
|
(void)CreateOfflineDevice(); // ignore the error (check for mOfflineDevice instead)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOfflineDevice) {
|
|
|
|
entry->MarkBinding();
|
|
|
|
nsresult rv = mOfflineDevice->BindEntry(entry);
|
|
|
|
entry->ClearBinding();
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
device = mOfflineDevice;
|
|
|
|
}
|
|
|
|
}
|
2007-05-15 00:09:20 +04:00
|
|
|
#endif // ! NECKO_OFFLINE_CACHE
|
2007-03-14 04:52:07 +03:00
|
|
|
|
2005-09-23 23:21:36 +04:00
|
|
|
if (device)
|
|
|
|
entry->SetCacheDevice(device);
|
2001-03-05 13:45:33 +03:00
|
|
|
return device;
|
2001-02-28 07:10:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsCacheService::DoomEntry(nsCacheEntry * entry)
|
|
|
|
{
|
2002-08-07 05:13:29 +04:00
|
|
|
return gService->DoomEntry_Internal(entry);
|
2001-02-28 07:10:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2002-08-07 05:13:29 +04:00
|
|
|
nsCacheService::DoomEntry_Internal(nsCacheEntry * entry)
|
2001-02-28 07:10:43 +03:00
|
|
|
{
|
|
|
|
if (entry->IsDoomed()) return NS_OK;
|
2001-04-04 07:30:45 +04:00
|
|
|
|
2008-09-12 15:44:19 +04:00
|
|
|
CACHE_LOG_DEBUG(("Dooming entry %p\n", entry));
|
2001-02-28 07:10:43 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
entry->MarkDoomed();
|
2001-04-04 07:30:45 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(!entry->IsBinding(), "Dooming entry while binding device.");
|
2001-02-28 07:10:43 +03:00
|
|
|
nsCacheDevice * device = entry->CacheDevice();
|
2001-03-08 08:37:00 +03:00
|
|
|
if (device) device->DoomEntry(entry);
|
2001-02-28 07:10:43 +03:00
|
|
|
|
2001-03-03 05:27:49 +03:00
|
|
|
if (entry->IsActive()) {
|
|
|
|
// remove from active entries
|
2001-03-08 08:37:00 +03:00
|
|
|
mActiveEntries.RemoveEntry(entry);
|
2008-09-12 15:44:19 +04:00
|
|
|
CACHE_LOG_DEBUG(("Removed entry %p from mActiveEntries\n", entry));
|
2001-03-03 05:27:49 +03:00
|
|
|
entry->MarkInactive();
|
2001-03-08 08:37:00 +03:00
|
|
|
}
|
2001-03-05 13:45:33 +03:00
|
|
|
|
2001-02-28 07:10:43 +03:00
|
|
|
// put on doom list to wait for descriptors to close
|
2001-03-08 08:37:00 +03:00
|
|
|
NS_ASSERTION(PR_CLIST_IS_EMPTY(entry), "doomed entry still on device list");
|
2001-03-05 10:17:58 +03:00
|
|
|
PR_APPEND_LINK(entry, &mDoomedEntries);
|
|
|
|
|
2001-03-08 08:37:00 +03:00
|
|
|
// tell pending requests to get on with their lives...
|
2001-03-05 13:45:33 +03:00
|
|
|
rv = ProcessPendingRequests(entry);
|
2001-03-08 08:37:00 +03:00
|
|
|
|
|
|
|
// All requests have been removed, but there may still be open descriptors
|
|
|
|
if (entry->IsNotInUse()) {
|
|
|
|
DeactivateEntry(entry); // tell device to get rid of it
|
|
|
|
}
|
2001-02-28 07:10:43 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-03-01 08:01:43 +03:00
|
|
|
|
2001-06-28 05:30:26 +04:00
|
|
|
void
|
2001-08-17 04:01:01 +04:00
|
|
|
nsCacheService::OnProfileShutdown(PRBool cleanse)
|
2001-06-28 05:30:26 +04:00
|
|
|
{
|
|
|
|
if (!gService) return;
|
2010-01-09 19:32:00 +03:00
|
|
|
if (!gService->mInitialized) {
|
|
|
|
// The cache service has been shut down, but someone is still holding
|
|
|
|
// a reference to it. Ignore this call.
|
|
|
|
return;
|
|
|
|
}
|
2006-06-28 03:13:48 +04:00
|
|
|
nsCacheServiceAutoLock lock;
|
2001-06-28 05:30:26 +04:00
|
|
|
|
2003-05-23 09:44:15 +04:00
|
|
|
gService->DoomActiveEntries();
|
|
|
|
gService->ClearDoomList();
|
|
|
|
|
2003-04-23 07:47:43 +04:00
|
|
|
#ifdef NECKO_DISK_CACHE
|
2003-05-23 09:44:15 +04:00
|
|
|
if (gService->mDiskDevice && gService->mEnableDiskDevice) {
|
2001-08-17 04:01:01 +04:00
|
|
|
if (cleanse)
|
|
|
|
gService->mDiskDevice->EvictEntries(nsnull);
|
2001-09-18 00:47:09 +04:00
|
|
|
|
2001-08-17 04:01:01 +04:00
|
|
|
gService->mDiskDevice->Shutdown();
|
|
|
|
}
|
2007-06-26 07:25:31 +04:00
|
|
|
gService->mEnableDiskDevice = PR_FALSE;
|
2007-05-15 00:09:20 +04:00
|
|
|
#endif // !NECKO_DISK_CACHE
|
2007-03-14 04:52:07 +03:00
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
#ifdef NECKO_OFFLINE_CACHE
|
2007-03-14 04:52:07 +03:00
|
|
|
if (gService->mOfflineDevice && gService->mEnableOfflineDevice) {
|
|
|
|
if (cleanse)
|
|
|
|
gService->mOfflineDevice->EvictEntries(nsnull);
|
|
|
|
|
|
|
|
gService->mOfflineDevice->Shutdown();
|
|
|
|
}
|
2007-06-26 07:25:31 +04:00
|
|
|
gService->mEnableOfflineDevice = PR_FALSE;
|
2007-05-15 00:09:20 +04:00
|
|
|
#endif // !NECKO_OFFLINE_CACHE
|
2003-04-23 09:15:48 +04:00
|
|
|
|
2001-08-17 04:01:01 +04:00
|
|
|
if (gService->mMemoryDevice) {
|
2003-04-23 09:15:48 +04:00
|
|
|
// clear memory cache
|
|
|
|
gService->mMemoryDevice->EvictEntries(nsnull);
|
|
|
|
}
|
|
|
|
|
2001-08-17 04:01:01 +04:00
|
|
|
}
|
2001-06-28 05:30:26 +04:00
|
|
|
|
2001-08-17 04:01:01 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
nsCacheService::OnProfileChanged()
|
|
|
|
{
|
|
|
|
if (!gService) return;
|
2008-09-12 15:44:19 +04:00
|
|
|
|
|
|
|
CACHE_LOG_DEBUG(("nsCacheService::OnProfileChanged"));
|
2001-08-17 04:01:01 +04:00
|
|
|
|
2006-06-28 03:13:48 +04:00
|
|
|
nsCacheServiceAutoLock lock;
|
2001-08-17 04:01:01 +04:00
|
|
|
|
2007-03-14 04:52:07 +03:00
|
|
|
gService->mEnableDiskDevice = gService->mObserver->DiskCacheEnabled();
|
|
|
|
gService->mEnableOfflineDevice = gService->mObserver->OfflineCacheEnabled();
|
|
|
|
gService->mEnableMemoryDevice = gService->mObserver->MemoryCacheEnabled();
|
|
|
|
|
2003-04-23 07:47:43 +04:00
|
|
|
#ifdef NECKO_DISK_CACHE
|
2001-08-17 04:01:01 +04:00
|
|
|
if (gService->mDiskDevice) {
|
|
|
|
gService->mDiskDevice->SetCacheParentDirectory(gService->mObserver->DiskCacheParentDirectory());
|
2001-06-28 05:30:26 +04:00
|
|
|
gService->mDiskDevice->SetCapacity(gService->mObserver->DiskCacheCapacity());
|
2001-08-17 04:01:01 +04:00
|
|
|
|
|
|
|
// XXX initialization of mDiskDevice could be made lazily, if mEnableDiskDevice is false
|
2005-10-12 06:18:32 +04:00
|
|
|
nsresult rv = gService->mDiskDevice->Init();
|
2001-08-17 04:01:01 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ERROR("nsCacheService::OnProfileChanged: Re-initializing disk device failed");
|
|
|
|
gService->mEnableDiskDevice = PR_FALSE;
|
|
|
|
// XXX delete mDiskDevice?
|
|
|
|
}
|
|
|
|
}
|
2007-05-15 00:09:20 +04:00
|
|
|
#endif // !NECKO_DISK_CACHE
|
2007-03-14 04:52:07 +03:00
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
#ifdef NECKO_OFFLINE_CACHE
|
2007-03-14 04:52:07 +03:00
|
|
|
if (gService->mOfflineDevice) {
|
|
|
|
gService->mOfflineDevice->SetCacheParentDirectory(gService->mObserver->OfflineCacheParentDirectory());
|
|
|
|
gService->mOfflineDevice->SetCapacity(gService->mObserver->OfflineCacheCapacity());
|
|
|
|
|
|
|
|
// XXX initialization of mOfflineDevice could be made lazily, if mEnableOfflineDevice is false
|
|
|
|
nsresult rv = gService->mOfflineDevice->Init();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ERROR("nsCacheService::OnProfileChanged: Re-initializing offline device failed");
|
|
|
|
gService->mEnableOfflineDevice = PR_FALSE;
|
|
|
|
// XXX delete mOfflineDevice?
|
|
|
|
}
|
|
|
|
}
|
2007-05-15 00:09:20 +04:00
|
|
|
#endif // !NECKO_OFFLINE_CACHE
|
2001-06-28 05:30:26 +04:00
|
|
|
|
2005-10-12 06:18:32 +04:00
|
|
|
// If memoryDevice exists, reset its size to the new profile
|
2001-08-17 04:01:01 +04:00
|
|
|
if (gService->mMemoryDevice) {
|
2005-10-12 06:18:32 +04:00
|
|
|
if (gService->mEnableMemoryDevice) {
|
|
|
|
// make sure that capacity is reset to the right value
|
2008-09-12 15:44:19 +04:00
|
|
|
PRInt32 capacity = gService->mObserver->MemoryCacheCapacity();
|
|
|
|
CACHE_LOG_DEBUG(("Resetting memory device capacity to %d\n",
|
|
|
|
capacity));
|
|
|
|
gService->mMemoryDevice->SetCapacity(capacity);
|
2005-10-12 06:18:32 +04:00
|
|
|
} else {
|
|
|
|
// tell memory device to evict everything
|
2008-09-12 15:44:19 +04:00
|
|
|
CACHE_LOG_DEBUG(("memory device disabled\n"));
|
2005-10-12 06:18:32 +04:00
|
|
|
gService->mMemoryDevice->SetCapacity(0);
|
|
|
|
// Don't delete memory device, because some entries may be active still...
|
2001-08-17 04:01:01 +04:00
|
|
|
}
|
|
|
|
}
|
2001-06-28 05:30:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCacheService::SetDiskCacheEnabled(PRBool enabled)
|
|
|
|
{
|
|
|
|
if (!gService) return;
|
2006-06-28 03:13:48 +04:00
|
|
|
nsCacheServiceAutoLock lock;
|
2001-06-28 05:30:26 +04:00
|
|
|
gService->mEnableDiskDevice = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCacheService::SetDiskCacheCapacity(PRInt32 capacity)
|
|
|
|
{
|
|
|
|
if (!gService) return;
|
2006-06-28 03:13:48 +04:00
|
|
|
nsCacheServiceAutoLock lock;
|
2001-06-28 05:30:26 +04:00
|
|
|
|
2003-04-23 07:47:43 +04:00
|
|
|
#ifdef NECKO_DISK_CACHE
|
2001-06-28 05:30:26 +04:00
|
|
|
if (gService->mDiskDevice) {
|
|
|
|
gService->mDiskDevice->SetCapacity(capacity);
|
|
|
|
}
|
2003-04-23 07:47:43 +04:00
|
|
|
#endif // !NECKO_DISK_CACHE
|
2001-06-28 05:30:26 +04:00
|
|
|
|
|
|
|
gService->mEnableDiskDevice = gService->mObserver->DiskCacheEnabled();
|
|
|
|
}
|
|
|
|
|
2007-03-14 04:52:07 +03:00
|
|
|
void
|
|
|
|
nsCacheService::SetOfflineCacheEnabled(PRBool enabled)
|
|
|
|
{
|
|
|
|
if (!gService) return;
|
|
|
|
nsCacheServiceAutoLock lock;
|
|
|
|
gService->mEnableOfflineDevice = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCacheService::SetOfflineCacheCapacity(PRInt32 capacity)
|
|
|
|
{
|
|
|
|
if (!gService) return;
|
|
|
|
nsCacheServiceAutoLock lock;
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
#ifdef NECKO_OFFLINE_CACHE
|
2007-03-14 04:52:07 +03:00
|
|
|
if (gService->mOfflineDevice) {
|
|
|
|
gService->mOfflineDevice->SetCapacity(capacity);
|
|
|
|
}
|
2007-05-15 00:09:20 +04:00
|
|
|
#endif // !NECKO_OFFLINE_CACHE
|
2007-03-14 04:52:07 +03:00
|
|
|
|
|
|
|
gService->mEnableOfflineDevice = gService->mObserver->OfflineCacheEnabled();
|
|
|
|
}
|
|
|
|
|
2001-06-28 05:30:26 +04:00
|
|
|
|
|
|
|
void
|
2005-10-09 23:58:06 +04:00
|
|
|
nsCacheService::SetMemoryCache()
|
2001-06-28 05:30:26 +04:00
|
|
|
{
|
|
|
|
if (!gService) return;
|
2008-09-12 15:44:19 +04:00
|
|
|
|
|
|
|
CACHE_LOG_DEBUG(("nsCacheService::SetMemoryCache"));
|
|
|
|
|
2006-06-28 03:13:48 +04:00
|
|
|
nsCacheServiceAutoLock lock;
|
2005-09-23 23:21:36 +04:00
|
|
|
|
2005-10-09 23:58:06 +04:00
|
|
|
gService->mEnableMemoryDevice = gService->mObserver->MemoryCacheEnabled();
|
|
|
|
|
|
|
|
if (gService->mEnableMemoryDevice) {
|
|
|
|
if (gService->mMemoryDevice) {
|
2008-09-12 16:13:10 +04:00
|
|
|
PRInt32 capacity = gService->mObserver->MemoryCacheCapacity();
|
2005-10-09 23:58:06 +04:00
|
|
|
// make sure that capacity is reset to the right value
|
2008-09-12 15:44:19 +04:00
|
|
|
CACHE_LOG_DEBUG(("Resetting memory device capacity to %d\n",
|
|
|
|
capacity));
|
|
|
|
gService->mMemoryDevice->SetCapacity(capacity);
|
2005-09-23 23:21:36 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (gService->mMemoryDevice) {
|
|
|
|
// tell memory device to evict everything
|
2008-09-12 15:44:19 +04:00
|
|
|
CACHE_LOG_DEBUG(("memory device disabled\n"));
|
2005-09-23 23:21:36 +04:00
|
|
|
gService->mMemoryDevice->SetCapacity(0);
|
2005-10-09 23:58:06 +04:00
|
|
|
// Don't delete memory device, because some entries may be active still...
|
2005-09-23 23:21:36 +04:00
|
|
|
}
|
2001-06-28 05:30:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
/******************************************************************************
|
|
|
|
* static methods for nsCacheEntryDescriptor
|
|
|
|
*****************************************************************************/
|
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCacheService::CloseDescriptor(nsCacheEntryDescriptor * descriptor)
|
2001-03-01 08:01:43 +03:00
|
|
|
{
|
2002-08-07 05:13:29 +04:00
|
|
|
// ask entry to remove descriptor
|
|
|
|
nsCacheEntry * entry = descriptor->CacheEntry();
|
|
|
|
PRBool stillActive = entry->RemoveDescriptor(descriptor);
|
|
|
|
nsresult rv = NS_OK;
|
2001-03-01 08:01:43 +03:00
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
if (!entry->IsValid()) {
|
|
|
|
rv = gService->ProcessPendingRequests(entry);
|
|
|
|
}
|
2001-03-01 08:01:43 +03:00
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
if (!stillActive) {
|
|
|
|
gService->DeactivateEntry(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsCacheService::GetFileForEntry(nsCacheEntry * entry,
|
|
|
|
nsIFile ** result)
|
|
|
|
{
|
|
|
|
nsCacheDevice * device = gService->EnsureEntryHasDevice(entry);
|
|
|
|
if (!device) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
return device->GetFileForEntry(entry, result);
|
2001-03-01 08:01:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2003-01-18 05:15:14 +03:00
|
|
|
nsCacheService::OpenInputStreamForEntry(nsCacheEntry * entry,
|
|
|
|
nsCacheAccessMode mode,
|
|
|
|
PRUint32 offset,
|
|
|
|
nsIInputStream ** result)
|
2001-02-28 07:10:43 +03:00
|
|
|
{
|
2002-08-07 05:13:29 +04:00
|
|
|
nsCacheDevice * device = gService->EnsureEntryHasDevice(entry);
|
|
|
|
if (!device) return NS_ERROR_UNEXPECTED;
|
2001-02-28 07:10:43 +03:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
return device->OpenInputStreamForEntry(entry, mode, offset, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsCacheService::OpenOutputStreamForEntry(nsCacheEntry * entry,
|
|
|
|
nsCacheAccessMode mode,
|
|
|
|
PRUint32 offset,
|
|
|
|
nsIOutputStream ** result)
|
|
|
|
{
|
|
|
|
nsCacheDevice * device = gService->EnsureEntryHasDevice(entry);
|
|
|
|
if (!device) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
return device->OpenOutputStreamForEntry(entry, mode, offset, result);
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
nsresult
|
|
|
|
nsCacheService::OnDataSizeChange(nsCacheEntry * entry, PRInt32 deltaSize)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2002-08-07 05:13:29 +04:00
|
|
|
nsCacheDevice * device = gService->EnsureEntryHasDevice(entry);
|
|
|
|
if (!device) return NS_ERROR_UNEXPECTED;
|
2001-02-23 16:18:01 +03:00
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
return device->OnDataSizeChange(entry, deltaSize);
|
|
|
|
}
|
2001-03-05 10:17:58 +03:00
|
|
|
|
2006-06-28 03:13:48 +04:00
|
|
|
void
|
|
|
|
nsCacheService::Lock()
|
|
|
|
{
|
|
|
|
PR_Lock(gService->mLock);
|
|
|
|
|
|
|
|
#if defined(DEBUG)
|
|
|
|
gService->mLockedThread = PR_GetCurrentThread();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCacheService::Unlock()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(gService->mLockedThread == PR_GetCurrentThread(), "oops");
|
|
|
|
|
|
|
|
nsTArray<nsISupports*> doomed;
|
|
|
|
doomed.SwapElements(gService->mDoomedObjects);
|
|
|
|
|
|
|
|
#if defined(DEBUG)
|
|
|
|
gService->mLockedThread = nsnull;
|
|
|
|
#endif
|
|
|
|
PR_Unlock(gService->mLock);
|
2001-03-05 10:17:58 +03:00
|
|
|
|
2006-06-28 03:14:50 +04:00
|
|
|
for (PRUint32 i = 0; i < doomed.Length(); ++i)
|
2006-06-28 03:13:48 +04:00
|
|
|
doomed[i]->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCacheService::ReleaseObject_Locked(nsISupports * obj,
|
|
|
|
nsIEventTarget * target)
|
2002-08-07 05:13:29 +04:00
|
|
|
{
|
2006-06-28 03:13:48 +04:00
|
|
|
NS_ASSERTION(gService->mLockedThread == PR_GetCurrentThread(), "oops");
|
|
|
|
|
|
|
|
PRBool isCur;
|
2010-07-30 07:16:37 +04:00
|
|
|
if (!target || NS_SUCCEEDED(target->IsOnCurrentThread(&isCur)) && isCur) {
|
2006-06-28 03:13:48 +04:00
|
|
|
gService->mDoomedObjects.AppendElement(obj);
|
|
|
|
} else {
|
|
|
|
NS_ProxyRelease(target, obj);
|
|
|
|
}
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
nsresult
|
|
|
|
nsCacheService::SetCacheElement(nsCacheEntry * entry, nsISupports * element)
|
2001-03-09 00:22:18 +03:00
|
|
|
{
|
2002-08-07 05:13:29 +04:00
|
|
|
entry->SetData(element);
|
|
|
|
entry->TouchData();
|
|
|
|
return NS_OK;
|
2001-03-09 00:22:18 +03:00
|
|
|
}
|
|
|
|
|
2001-10-19 06:05:12 +04:00
|
|
|
|
2002-08-07 05:13:29 +04:00
|
|
|
nsresult
|
|
|
|
nsCacheService::ValidateEntry(nsCacheEntry * entry)
|
|
|
|
{
|
|
|
|
nsCacheDevice * device = gService->EnsureEntryHasDevice(entry);
|
|
|
|
if (!device) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
entry->MarkValid();
|
|
|
|
nsresult rv = gService->ProcessPendingRequests(entry);
|
|
|
|
NS_ASSERTION(rv == NS_OK, "ProcessPendingRequests failed.");
|
|
|
|
// XXX what else should be done?
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2001-02-23 16:18:01 +03:00
|
|
|
void
|
2001-02-26 17:55:58 +03:00
|
|
|
nsCacheService::DeactivateEntry(nsCacheEntry * entry)
|
2001-02-23 16:18:01 +03:00
|
|
|
{
|
2008-09-12 15:44:19 +04:00
|
|
|
CACHE_LOG_DEBUG(("Deactivating entry %p\n", entry));
|
2001-03-02 04:51:41 +03:00
|
|
|
nsresult rv = NS_OK;
|
2001-03-13 18:43:17 +03:00
|
|
|
NS_ASSERTION(entry->IsNotInUse(), "### deactivating an entry while in use!");
|
2001-10-19 06:05:12 +04:00
|
|
|
nsCacheDevice * device = nsnull;
|
2001-03-13 18:43:17 +03:00
|
|
|
|
|
|
|
if (mMaxDataSize < entry->DataSize() ) mMaxDataSize = entry->DataSize();
|
|
|
|
if (mMaxMetaSize < entry->MetaDataSize() ) mMaxMetaSize = entry->MetaDataSize();
|
2001-03-02 04:51:41 +03:00
|
|
|
|
|
|
|
if (entry->IsDoomed()) {
|
|
|
|
// remove from Doomed list
|
2001-03-05 10:17:58 +03:00
|
|
|
PR_REMOVE_AND_INIT_LINK(entry);
|
2001-10-19 06:05:12 +04:00
|
|
|
} else if (entry->IsActive()) {
|
|
|
|
// remove from active entries
|
|
|
|
mActiveEntries.RemoveEntry(entry);
|
2008-09-12 15:44:19 +04:00
|
|
|
CACHE_LOG_DEBUG(("Removed deactivated entry %p from mActiveEntries\n",
|
|
|
|
entry));
|
2001-10-19 06:05:12 +04:00
|
|
|
entry->MarkInactive();
|
|
|
|
|
|
|
|
// bind entry if necessary to store meta-data
|
|
|
|
device = EnsureEntryHasDevice(entry);
|
|
|
|
if (!device) {
|
2008-09-12 15:44:19 +04:00
|
|
|
CACHE_LOG_DEBUG(("DeactivateEntry: unable to bind active "
|
|
|
|
"entry %p\n",
|
|
|
|
entry));
|
2001-10-19 06:05:12 +04:00
|
|
|
NS_WARNING("DeactivateEntry: unable to bind active entry\n");
|
|
|
|
return;
|
2001-03-03 05:27:49 +03:00
|
|
|
}
|
2001-10-19 06:05:12 +04:00
|
|
|
} else {
|
2002-08-07 05:13:29 +04:00
|
|
|
// if mInitialized == PR_FALSE,
|
2002-02-19 08:32:18 +03:00
|
|
|
// then we're shutting down and this state is okay.
|
2002-08-07 05:13:29 +04:00
|
|
|
NS_ASSERTION(!mInitialized, "DeactivateEntry: bad cache entry state.");
|
2001-03-02 04:51:41 +03:00
|
|
|
}
|
|
|
|
|
2001-10-19 06:05:12 +04:00
|
|
|
device = entry->CacheDevice();
|
2001-02-26 17:55:58 +03:00
|
|
|
if (device) {
|
2001-03-02 04:51:41 +03:00
|
|
|
rv = device->DeactivateEntry(entry);
|
2001-02-27 08:35:53 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2001-03-02 04:51:41 +03:00
|
|
|
// increment deactivate failure count
|
|
|
|
++mDeactivateFailures;
|
2001-02-27 08:35:53 +03:00
|
|
|
}
|
2001-02-26 17:55:58 +03:00
|
|
|
} else {
|
2001-03-02 04:51:41 +03:00
|
|
|
// increment deactivating unbound entry statistic
|
|
|
|
++mDeactivatedUnboundEntries;
|
2001-03-03 05:27:49 +03:00
|
|
|
delete entry; // because no one else will
|
2001-02-26 17:55:58 +03:00
|
|
|
}
|
2001-02-23 16:18:01 +03:00
|
|
|
}
|
2001-03-04 03:11:30 +03:00
|
|
|
|
2001-03-05 13:45:33 +03:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsCacheService::ProcessPendingRequests(nsCacheEntry * entry)
|
|
|
|
{
|
2001-06-27 06:30:18 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCacheRequest * request = (nsCacheRequest *)PR_LIST_HEAD(&entry->mRequestQ);
|
|
|
|
nsCacheRequest * nextRequest;
|
|
|
|
PRBool newWriter = PR_FALSE;
|
|
|
|
|
|
|
|
if (request == &entry->mRequestQ) return NS_OK; // no queued requests
|
2001-03-05 13:45:33 +03:00
|
|
|
|
|
|
|
if (!entry->IsDoomed() && entry->IsInvalid()) {
|
|
|
|
// 1st descriptor closed w/o MarkValid()
|
2001-06-27 06:30:18 +04:00
|
|
|
NS_ASSERTION(PR_CLIST_IS_EMPTY(&entry->mDescriptorQ), "shouldn't be here with open descriptors");
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
// verify no ACCESS_WRITE requests(shouldn't have any of these)
|
|
|
|
while (request != &entry->mRequestQ) {
|
|
|
|
NS_ASSERTION(request->AccessRequested() != nsICache::ACCESS_WRITE,
|
|
|
|
"ACCESS_WRITE request should have been given a new entry");
|
|
|
|
request = (nsCacheRequest *)PR_NEXT_LINK(request);
|
|
|
|
}
|
|
|
|
request = (nsCacheRequest *)PR_LIST_HEAD(&entry->mRequestQ);
|
|
|
|
#endif
|
|
|
|
// find first request with ACCESS_READ_WRITE (if any) and promote it to 1st writer
|
|
|
|
while (request != &entry->mRequestQ) {
|
|
|
|
if (request->AccessRequested() == nsICache::ACCESS_READ_WRITE) {
|
|
|
|
newWriter = PR_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
request = (nsCacheRequest *)PR_NEXT_LINK(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request == &entry->mRequestQ) // no requests asked for ACCESS_READ_WRITE, back to top
|
|
|
|
request = (nsCacheRequest *)PR_LIST_HEAD(&entry->mRequestQ);
|
|
|
|
|
|
|
|
// XXX what should we do if there are only READ requests in queue?
|
|
|
|
// XXX serialize their accesses, give them only read access, but force them to check validate flag?
|
|
|
|
// XXX or do readers simply presume the entry is valid
|
2001-03-05 13:45:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCacheAccessMode accessGranted = nsICache::ACCESS_NONE;
|
|
|
|
|
|
|
|
while (request != &entry->mRequestQ) {
|
|
|
|
nextRequest = (nsCacheRequest *)PR_NEXT_LINK(request);
|
|
|
|
|
|
|
|
if (request->mListener) {
|
|
|
|
|
|
|
|
// Async request
|
|
|
|
PR_REMOVE_AND_INIT_LINK(request);
|
|
|
|
|
|
|
|
if (entry->IsDoomed()) {
|
2001-05-09 07:36:00 +04:00
|
|
|
rv = ProcessRequest(request, PR_FALSE, nsnull);
|
2001-03-05 13:45:33 +03:00
|
|
|
if (rv == NS_ERROR_CACHE_WAIT_FOR_VALIDATION)
|
|
|
|
rv = NS_OK;
|
2001-03-08 08:37:00 +03:00
|
|
|
else
|
|
|
|
delete request;
|
2001-03-05 13:45:33 +03:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// XXX what to do?
|
|
|
|
}
|
2001-06-27 06:30:18 +04:00
|
|
|
} else if (entry->IsValid() || newWriter) {
|
2001-03-05 13:45:33 +03:00
|
|
|
rv = entry->RequestAccess(request, &accessGranted);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"if entry is valid, RequestAccess must succeed.");
|
2001-06-27 06:30:18 +04:00
|
|
|
// XXX if (newWriter) NS_ASSERTION( accessGranted == request->AccessRequested(), "why not?");
|
2001-03-05 13:45:33 +03:00
|
|
|
|
|
|
|
// entry->CreateDescriptor dequeues request, and queues descriptor
|
2006-06-28 03:13:48 +04:00
|
|
|
nsICacheEntryDescriptor *descriptor = nsnull;
|
2001-03-08 08:37:00 +03:00
|
|
|
rv = entry->CreateDescriptor(request,
|
|
|
|
accessGranted,
|
2006-06-28 03:13:48 +04:00
|
|
|
&descriptor);
|
2001-03-05 13:45:33 +03:00
|
|
|
|
|
|
|
// post call to listener to report error or descriptor
|
|
|
|
rv = NotifyListener(request, descriptor, accessGranted, rv);
|
2001-03-08 08:37:00 +03:00
|
|
|
delete request;
|
2001-03-05 13:45:33 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// XXX what to do?
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// XXX bad state
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Synchronous request
|
|
|
|
request->WakeUp();
|
|
|
|
}
|
2001-06-27 06:30:18 +04:00
|
|
|
if (newWriter) break; // process remaining requests after validation
|
2001-03-05 13:45:33 +03:00
|
|
|
request = nextRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-03-13 18:43:17 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
nsCacheService::ClearPendingRequests(nsCacheEntry * entry)
|
|
|
|
{
|
|
|
|
nsCacheRequest * request = (nsCacheRequest *)PR_LIST_HEAD(&entry->mRequestQ);
|
|
|
|
|
|
|
|
while (request != &entry->mRequestQ) {
|
|
|
|
nsCacheRequest * next = (nsCacheRequest *)PR_NEXT_LINK(request);
|
|
|
|
|
|
|
|
// XXX we're just dropping these on the floor for now...definitely wrong.
|
|
|
|
PR_REMOVE_AND_INIT_LINK(request);
|
|
|
|
delete request;
|
|
|
|
request = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCacheService::ClearDoomList()
|
|
|
|
{
|
|
|
|
nsCacheEntry * entry = (nsCacheEntry *)PR_LIST_HEAD(&mDoomedEntries);
|
|
|
|
|
|
|
|
while (entry != &mDoomedEntries) {
|
|
|
|
nsCacheEntry * next = (nsCacheEntry *)PR_NEXT_LINK(entry);
|
|
|
|
|
|
|
|
entry->DetachDescriptors();
|
|
|
|
DeactivateEntry(entry);
|
|
|
|
entry = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCacheService::ClearActiveEntries()
|
|
|
|
{
|
2008-03-11 11:08:05 +03:00
|
|
|
mActiveEntries.VisitEntries(DeactivateAndClearEntry, nsnull);
|
2001-05-03 06:35:26 +04:00
|
|
|
mActiveEntries.Shutdown();
|
2001-03-13 18:43:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
PLDHashOperator
|
2001-03-22 07:20:20 +03:00
|
|
|
nsCacheService::DeactivateAndClearEntry(PLDHashTable * table,
|
|
|
|
PLDHashEntryHdr * hdr,
|
|
|
|
PRUint32 number,
|
|
|
|
void * arg)
|
2001-03-13 18:43:17 +03:00
|
|
|
{
|
|
|
|
nsCacheEntry * entry = ((nsCacheEntryHashTableEntry *)hdr)->cacheEntry;
|
|
|
|
NS_ASSERTION(entry, "### active entry = nsnull!");
|
|
|
|
gService->ClearPendingRequests(entry);
|
|
|
|
entry->DetachDescriptors();
|
2001-05-08 04:13:21 +04:00
|
|
|
|
|
|
|
entry->MarkInactive(); // so we don't call Remove() while we're enumerating
|
2001-03-13 18:43:17 +03:00
|
|
|
gService->DeactivateEntry(entry);
|
|
|
|
|
2001-04-23 09:15:03 +04:00
|
|
|
return PL_DHASH_REMOVE; // and continue enumerating
|
2001-03-13 18:43:17 +03:00
|
|
|
}
|
|
|
|
|
2001-09-18 00:47:09 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
nsCacheService::DoomActiveEntries()
|
|
|
|
{
|
2009-02-23 04:05:28 +03:00
|
|
|
nsAutoTArray<nsCacheEntry*, 8> array;
|
2001-09-18 00:47:09 +04:00
|
|
|
|
2008-03-11 11:08:05 +03:00
|
|
|
mActiveEntries.VisitEntries(RemoveActiveEntry, &array);
|
2001-09-18 00:47:09 +04:00
|
|
|
|
2009-02-23 04:05:28 +03:00
|
|
|
PRUint32 count = array.Length();
|
2001-09-18 00:47:09 +04:00
|
|
|
for (PRUint32 i=0; i < count; ++i)
|
2009-02-23 04:05:28 +03:00
|
|
|
DoomEntry_Internal(array[i]);
|
2001-09-18 00:47:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
PLDHashOperator
|
2001-09-18 00:47:09 +04:00
|
|
|
nsCacheService::RemoveActiveEntry(PLDHashTable * table,
|
|
|
|
PLDHashEntryHdr * hdr,
|
|
|
|
PRUint32 number,
|
|
|
|
void * arg)
|
|
|
|
{
|
|
|
|
nsCacheEntry * entry = ((nsCacheEntryHashTableEntry *)hdr)->cacheEntry;
|
|
|
|
NS_ASSERTION(entry, "### active entry = nsnull!");
|
|
|
|
|
2009-02-23 04:05:28 +03:00
|
|
|
nsTArray<nsCacheEntry*> * array = (nsTArray<nsCacheEntry*> *) arg;
|
2001-09-18 00:47:09 +04:00
|
|
|
NS_ASSERTION(array, "### array = nsnull!");
|
|
|
|
array->AppendElement(entry);
|
|
|
|
|
|
|
|
// entry is being removed from the active entry list
|
|
|
|
entry->MarkInactive();
|
|
|
|
return PL_DHASH_REMOVE; // and continue enumerating
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-04 07:30:45 +04:00
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
void
|
|
|
|
nsCacheService::LogCacheStatistics()
|
|
|
|
{
|
|
|
|
PRUint32 hitPercentage = (PRUint32)((((double)mCacheHits) /
|
|
|
|
((double)(mCacheHits + mCacheMisses))) * 100);
|
|
|
|
CACHE_LOG_ALWAYS(("\nCache Service Statistics:\n\n"));
|
|
|
|
CACHE_LOG_ALWAYS((" TotalEntries = %d\n", mTotalEntries));
|
|
|
|
CACHE_LOG_ALWAYS((" Cache Hits = %d\n", mCacheHits));
|
|
|
|
CACHE_LOG_ALWAYS((" Cache Misses = %d\n", mCacheMisses));
|
|
|
|
CACHE_LOG_ALWAYS((" Cache Hit %% = %d%%\n", hitPercentage));
|
|
|
|
CACHE_LOG_ALWAYS((" Max Key Length = %d\n", mMaxKeyLength));
|
|
|
|
CACHE_LOG_ALWAYS((" Max Meta Size = %d\n", mMaxMetaSize));
|
|
|
|
CACHE_LOG_ALWAYS((" Max Data Size = %d\n", mMaxDataSize));
|
|
|
|
CACHE_LOG_ALWAYS(("\n"));
|
2003-04-23 09:15:48 +04:00
|
|
|
CACHE_LOG_ALWAYS((" Deactivate Failures = %d\n",
|
|
|
|
mDeactivateFailures));
|
|
|
|
CACHE_LOG_ALWAYS((" Deactivated Unbound Entries = %d\n",
|
|
|
|
mDeactivatedUnboundEntries));
|
2001-04-04 07:30:45 +04:00
|
|
|
}
|
|
|
|
#endif
|
2008-10-25 14:26:20 +04:00
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCacheService::OnEnterExitPrivateBrowsing()
|
|
|
|
{
|
|
|
|
if (!gService) return;
|
|
|
|
nsCacheServiceAutoLock lock;
|
|
|
|
|
|
|
|
gService->DoomActiveEntries();
|
|
|
|
|
|
|
|
if (gService->mMemoryDevice) {
|
|
|
|
// clear memory cache
|
|
|
|
gService->mMemoryDevice->EvictEntries(nsnull);
|
|
|
|
}
|
|
|
|
}
|