Bug 442810 - update ui for app cache changes. r=gavin, r=honzab, sr=bz

This commit is contained in:
Dave Camp 2009-02-22 11:09:10 -08:00
parent 1278a3978f
commit cfb033a33d
6 changed files with 116 additions and 24 deletions

View File

@ -5429,10 +5429,25 @@ var OfflineApps = {
},
// XXX: duplicated in preferences/advanced.js
_getOfflineAppUsage: function (host)
_getOfflineAppUsage: function (host, groups)
{
// XXX Bug 442810: include offline cache usage.
var cacheService = Components.classes["@mozilla.org/network/application-cache-service;1"].
getService(Components.interfaces.nsIApplicationCacheService);
if (!groups) {
groups = cacheService.getGroups({});
}
var ios = Components.classes["@mozilla.org/network/io-service;1"].
getService(Components.interfaces.nsIIOService);
var usage = 0;
for (var i = 0; i < groups.length; i++) {
var uri = ios.newURI(groups[i], null, null);
if (uri.asciiHost == host) {
var cache = cacheService.getActiveCache(groups[i]);
usage += cache.usage;
}
}
var storageManager = Components.classes["@mozilla.org/dom/storagemanager;1"].
getService(Components.interfaces.nsIDOMStorageManager);
usage += storageManager.getUsage(host);

View File

@ -216,10 +216,24 @@ var gAdvancedPane = {
},
// XXX: duplicated in browser.js
_getOfflineAppUsage: function (host)
_getOfflineAppUsage: function (host, groups)
{
// XXX Bug 442710: include offline cache usage.
var cacheService = Components.classes["@mozilla.org/network/application-cache-service;1"].
getService(Components.interfaces.nsIApplicationCacheService);
if (!groups) {
groups = cacheService.getGroups({});
}
var ios = Components.classes["@mozilla.org/network/io-service;1"].
getService(Components.interfaces.nsIIOService);
var usage = 0;
for (var i = 0; i < groups.length; i++) {
var uri = ios.newURI(groups[i], null, null);
if (uri.asciiHost == host) {
var cache = cacheService.getActiveCache(groups[i]);
usage += cache.usage;
}
}
var storageManager = Components.classes["@mozilla.org/dom/storagemanager;1"].
getService(Components.interfaces.nsIDOMStorageManager);
@ -241,6 +255,10 @@ var gAdvancedPane = {
list.removeChild(list.firstChild);
}
var cacheService = Components.classes["@mozilla.org/network/application-cache-service;1"].
getService(Components.interfaces.nsIApplicationCacheService);
var groups = cacheService.getGroups({});
var bundle = document.getElementById("bundlePreferences");
var enumerator = pm.enumerator;
@ -254,7 +272,7 @@ var gAdvancedPane = {
row.className = "offlineapp";
row.setAttribute("host", perm.host);
var converted = DownloadUtils.
convertByteUnits(this._getOfflineAppUsage(perm.host));
convertByteUnits(this._getOfflineAppUsage(perm.host, groups));
row.setAttribute("usage",
bundle.getFormattedString("offlineAppUsage",
converted));
@ -295,14 +313,18 @@ var gAdvancedPane = {
return;
// clear offline cache entries
var cacheService = Components.classes["@mozilla.org/network/cache-service;1"]
.getService(Components.interfaces.nsICacheService);
var cacheSession = cacheService.createSession("HTTP-offline",
Components.interfaces.nsICache.STORE_OFFLINE,
true)
.QueryInterface(Components.interfaces.nsIOfflineCacheSession);
cacheSession.clearKeysOwnedByDomain(host);
cacheSession.evictUnownedEntries();
var cacheService = Components.classes["@mozilla.org/network/application-cache-service;1"].
getService(Components.interfaces.nsIApplicationCacheService);
var ios = Components.classes["@mozilla.org/network/io-service;1"].
getService(Components.interfaces.nsIIOService);
var groups = cacheService.getGroups({});
for (var i = 0; i < groups.length; i++) {
var uri = ios.newURI(groups[i], null, null);
if (uri.asciiHost == host) {
var cache = cacheService.getActiveCache(groups[i]);
cache.discard();
}
}
// send out an offline-app-removed signal. The nsDOMStorage
// service will clear DOM storage for this host.

View File

@ -111,7 +111,7 @@ interface nsIApplicationCacheNamespace : nsISupports
* loads. Inactive caches will be removed from the cache when they are
* no longer referenced.
*/
[scriptable, uuid(1e1a2371-875b-4e57-98ff-a81af1750ca2)]
[scriptable, uuid(663e2e2e-04a0-47b6-87b3-a122be46cb53)]
interface nsIApplicationCache : nsISupports
{
/**
@ -161,6 +161,11 @@ interface nsIApplicationCache : nsISupports
*/
readonly attribute boolean active;
/**
* The disk usage of the application cache, in bytes.
*/
readonly attribute unsigned long usage;
/**
* Makes this cache the active application cache for this group.
* Future loads associated with this group will come from this

View File

@ -45,7 +45,7 @@ interface nsIApplicationCache;
* The application cache service manages the set of application cache
* groups.
*/
[scriptable, uuid(36595ec8-e849-49f8-9eb4-e895a3cd39fe)]
[scriptable, uuid(611161c8-37d0-450f-a4fe-457c47bbaf64)]
interface nsIApplicationCacheService : nsISupports
{
/**
@ -87,4 +87,10 @@ interface nsIApplicationCacheService : nsISupports
* The cache entry key.
*/
void cacheOpportunistically(in nsIApplicationCache cache, in ACString key);
/**
* Get the list of application cache groups.
*/
void getGroups(out unsigned long count,
[array, size_is(count), retval] out string groupIDs);
};

View File

@ -757,6 +757,14 @@ nsApplicationCache::GetMatchingNamespace(const nsACString &key,
return mDevice->GetMatchingNamespace(mClientID, key, out);
}
NS_IMETHODIMP
nsApplicationCache::GetUsage(PRUint32 *usage)
{
NS_ENSURE_TRUE(mValid, NS_ERROR_NOT_AVAILABLE);
return mDevice->GetUsage(mClientID, usage);
}
/******************************************************************************
* nsOfflineCacheDevice
*/
@ -1077,8 +1085,7 @@ nsOfflineCacheDevice::Init()
statement (aStatement), sql (aSql) {}
} prepared[] = {
StatementSql ( mStatement_CacheSize, "SELECT Sum(DataSize) from moz_cache;" ),
// XXX bug 442810: Restore the ability to monitor individual cache usage
StatementSql ( mStatement_DomainSize, "SELECT 0;"),
StatementSql ( mStatement_ApplicationCacheSize, "SELECT Sum(DataSize) from moz_cache WHERE ClientID = ?;" ),
StatementSql ( mStatement_EntryCount, "SELECT count(*) from moz_cache;" ),
StatementSql ( mStatement_UpdateEntry, "UPDATE moz_cache SET MetaData = ?, Flags = ?, DataSize = ?, FetchCount = ?, LastFetched = ?, LastModified = ?, ExpirationTime = ? WHERE ClientID = ? AND Key = ?;" ),
StatementSql ( mStatement_UpdateEntrySize, "UPDATE moz_cache SET DataSize = ? WHERE ClientID = ? AND Key = ?;" ),
@ -1109,6 +1116,7 @@ nsOfflineCacheDevice::Init()
" AND NameSpace <= ?2 AND ?2 GLOB NameSpace || '*'"
" ORDER BY NameSpace DESC;"),
StatementSql ( mStatement_InsertNamespaceEntry, "INSERT INTO moz_cache_namespaces (ClientID, NameSpace, Data, ItemType) VALUES(?, ?, ?, ?);"),
StatementSql ( mStatement_EnumerateGroups, "SELECT GroupID, ActiveClientID FROM moz_cache_groups;")
};
for (PRUint32 i = 0; NS_SUCCEEDED(rv) && i < NS_ARRAY_LENGTH(prepared); ++i)
{
@ -1141,12 +1149,7 @@ nsOfflineCacheDevice::InitActiveCaches()
nsresult rv = mActiveCaches.Init(5);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<mozIStorageStatement> statement;
rv =
mDB->CreateStatement(NS_LITERAL_CSTRING("SELECT GroupID, ActiveClientID"
" FROM moz_cache_groups"),
getter_AddRefs(statement));
NS_ENSURE_SUCCESS(rv, rv);
AutoResetStatement statement(mStatement_EnumerateGroups);
PRBool hasRows;
rv = statement->ExecuteStep(&hasRows);
@ -1891,6 +1894,43 @@ nsOfflineCacheDevice::AddNamespace(const nsCString &clientID,
return NS_OK;
}
nsresult
nsOfflineCacheDevice::GetUsage(const nsACString &clientID,
PRUint32 *usage)
{
LOG(("nsOfflineCacheDevice::GetUsage [cid=%s]\n",
PromiseFlatCString(clientID).get()));
*usage = 0;
AutoResetStatement statement(mStatement_ApplicationCacheSize);
nsresult rv = statement->BindUTF8StringParameter(0, clientID);
NS_ENSURE_SUCCESS(rv, rv);
PRBool hasRows;
rv = statement->ExecuteStep(&hasRows);
NS_ENSURE_SUCCESS(rv, rv);
if (!hasRows)
return NS_OK;
*usage = static_cast<PRUint32>(statement->AsInt32(0));
return NS_OK;
}
NS_IMETHODIMP
nsOfflineCacheDevice::GetGroups(PRUint32 *count,
char ***keys)
{
LOG(("nsOfflineCacheDevice::GetGroups"));
AutoResetStatement statement(mStatement_EnumerateGroups);
return RunSimpleQuery(mStatement_EnumerateGroups, 0, count, keys);
}
nsresult
nsOfflineCacheDevice::RunSimpleQuery(mozIStorageStatement * statement,
PRUint32 resultIndex,

View File

@ -235,6 +235,9 @@ private:
nsresult AddNamespace(const nsCString &clientID,
nsIApplicationCacheNamespace *ns);
nsresult GetUsage(const nsACString &clientID,
PRUint32 *usage);
nsresult RunSimpleQuery(mozIStorageStatement *statment,
PRUint32 resultIndex,
PRUint32 * count,
@ -244,7 +247,7 @@ private:
nsRefPtr<nsOfflineCacheEvictionFunction> mEvictionFunction;
nsCOMPtr<mozIStorageStatement> mStatement_CacheSize;
nsCOMPtr<mozIStorageStatement> mStatement_DomainSize;
nsCOMPtr<mozIStorageStatement> mStatement_ApplicationCacheSize;
nsCOMPtr<mozIStorageStatement> mStatement_EntryCount;
nsCOMPtr<mozIStorageStatement> mStatement_UpdateEntry;
nsCOMPtr<mozIStorageStatement> mStatement_UpdateEntrySize;
@ -264,6 +267,7 @@ private:
nsCOMPtr<mozIStorageStatement> mStatement_DeactivateGroup;
nsCOMPtr<mozIStorageStatement> mStatement_FindClient;
nsCOMPtr<mozIStorageStatement> mStatement_FindClientByNamespace;
nsCOMPtr<mozIStorageStatement> mStatement_EnumerateGroups;
nsCOMPtr<nsILocalFile> mCacheDirectory;
PRUint32 mCacheCapacity; // in bytes