2012-07-17 04:38:47 +04:00
|
|
|
/* -*- Mode: C++; indent-tab-mode: nil; c-basic-offset: 2 -*- */
|
2004-10-20 09:00:23 +04:00
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cin: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
#include "mozilla/Util.h"
|
2012-06-06 07:18:25 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
#include "nsCache.h"
|
|
|
|
#include "nsDiskCache.h"
|
|
|
|
#include "nsDiskCacheDeviceSQL.h"
|
2007-05-15 00:09:20 +04:00
|
|
|
#include "nsCacheService.h"
|
2010-10-20 21:12:32 +04:00
|
|
|
#include "nsApplicationCache.h"
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2008-09-30 22:34:23 +04:00
|
|
|
#include "nsNetCID.h"
|
2004-10-20 09:00:23 +04:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2008-08-27 03:09:02 +04:00
|
|
|
#include "nsEscape.h"
|
2008-08-28 05:15:32 +04:00
|
|
|
#include "nsIPrefBranch.h"
|
|
|
|
#include "nsIPrefService.h"
|
2004-10-20 09:00:23 +04:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsPrintfCString.h"
|
|
|
|
#include "nsCRT.h"
|
2008-09-30 22:34:23 +04:00
|
|
|
#include "nsArrayUtils.h"
|
|
|
|
#include "nsIArray.h"
|
2007-07-08 01:14:56 +04:00
|
|
|
#include "nsIVariant.h"
|
2010-08-24 05:06:23 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
#include "mozIStorageService.h"
|
|
|
|
#include "mozIStorageStatement.h"
|
2007-05-15 00:09:20 +04:00
|
|
|
#include "mozIStorageFunction.h"
|
|
|
|
#include "mozStorageHelper.h"
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
#include "nsICacheVisitor.h"
|
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
|
2010-05-20 03:22:19 +04:00
|
|
|
#include "mozilla/FunctionTimer.h"
|
2011-10-30 18:39:53 +04:00
|
|
|
#include "mozilla/Telemetry.h"
|
2010-05-20 03:22:19 +04:00
|
|
|
|
2012-06-29 02:27:03 +04:00
|
|
|
#include "sqlite3.h"
|
|
|
|
#include "mozilla/storage.h"
|
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
using namespace mozilla;
|
2012-06-29 02:27:03 +04:00
|
|
|
using namespace mozilla::storage;
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
static const char OFFLINE_CACHE_DEVICE_ID[] = { "offline" };
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
#define LOG(args) CACHE_LOG_DEBUG(args)
|
|
|
|
|
2007-07-25 10:31:27 +04:00
|
|
|
static PRUint32 gNextTemporaryClientID = 0;
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* helpers
|
|
|
|
*/
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
EnsureDir(nsIFile *dir)
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool exists;
|
2004-10-20 09:00:23 +04:00
|
|
|
nsresult rv = dir->Exists(&exists);
|
|
|
|
if (NS_SUCCEEDED(rv) && !exists)
|
|
|
|
rv = dir->Create(nsIFile::DIRECTORY_TYPE, 0700);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2004-10-20 09:00:23 +04:00
|
|
|
DecomposeCacheEntryKey(const nsCString *fullKey,
|
|
|
|
const char **cid,
|
|
|
|
const char **key,
|
|
|
|
nsCString &buf)
|
|
|
|
{
|
|
|
|
buf = *fullKey;
|
|
|
|
|
|
|
|
PRInt32 colon = buf.FindChar(':');
|
|
|
|
if (colon == kNotFound)
|
|
|
|
{
|
|
|
|
NS_ERROR("Invalid key");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2004-10-20 09:00:23 +04:00
|
|
|
}
|
|
|
|
buf.SetCharAt('\0', colon);
|
|
|
|
|
|
|
|
*cid = buf.get();
|
|
|
|
*key = buf.get() + colon + 1;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2004-10-20 09:00:23 +04:00
|
|
|
}
|
|
|
|
|
2004-10-28 02:19:32 +04:00
|
|
|
class AutoResetStatement
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoResetStatement(mozIStorageStatement *s)
|
|
|
|
: mStatement(s) {}
|
|
|
|
~AutoResetStatement() { mStatement->Reset(); }
|
|
|
|
mozIStorageStatement *operator->() { return mStatement; }
|
|
|
|
private:
|
|
|
|
mozIStorageStatement *mStatement;
|
|
|
|
};
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
class EvictionObserver
|
|
|
|
{
|
|
|
|
public:
|
2007-07-26 04:47:33 +04:00
|
|
|
EvictionObserver(mozIStorageConnection *db,
|
|
|
|
nsOfflineCacheEvictionFunction *evictionFunction)
|
|
|
|
: mDB(db), mEvictionFunction(evictionFunction)
|
2007-05-15 00:09:20 +04:00
|
|
|
{
|
|
|
|
mDB->ExecuteSimpleSQL(
|
2012-06-29 02:27:03 +04:00
|
|
|
NS_LITERAL_CSTRING("CREATE TEMP TRIGGER cache_on_delete BEFORE DELETE"
|
2007-05-15 00:09:20 +04:00
|
|
|
" ON moz_cache FOR EACH ROW BEGIN SELECT"
|
|
|
|
" cache_eviction_observer("
|
2012-06-29 02:27:03 +04:00
|
|
|
" OLD.ClientID, OLD.key, OLD.generation);"
|
2007-05-15 00:09:20 +04:00
|
|
|
" END;"));
|
2007-07-26 04:47:33 +04:00
|
|
|
mEvictionFunction->Reset();
|
2007-05-15 00:09:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
~EvictionObserver()
|
|
|
|
{
|
|
|
|
mDB->ExecuteSimpleSQL(
|
|
|
|
NS_LITERAL_CSTRING("DROP TRIGGER cache_on_delete;"));
|
2007-07-26 04:47:33 +04:00
|
|
|
mEvictionFunction->Reset();
|
2007-05-15 00:09:20 +04:00
|
|
|
}
|
|
|
|
|
2007-07-26 04:47:33 +04:00
|
|
|
void Apply() { return mEvictionFunction->Apply(); }
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
private:
|
|
|
|
mozIStorageConnection *mDB;
|
2007-07-26 04:47:33 +04:00
|
|
|
nsRefPtr<nsOfflineCacheEvictionFunction> mEvictionFunction;
|
2007-05-15 00:09:20 +04:00
|
|
|
};
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
#define DCACHE_HASH_MAX LL_MAXINT
|
|
|
|
#define DCACHE_HASH_BITS 64
|
|
|
|
|
|
|
|
/**
|
2007-05-15 00:09:20 +04:00
|
|
|
* nsOfflineCache::Hash(const char * key)
|
2004-10-20 09:00:23 +04:00
|
|
|
*
|
2007-05-15 00:09:20 +04:00
|
|
|
* This algorithm of this method implies nsOfflineCacheRecords will be stored
|
2004-10-20 09:00:23 +04:00
|
|
|
* in a certain order on disk. If the algorithm changes, existing cache
|
|
|
|
* map files may become invalid, and therefore the kCurrentVersion needs
|
|
|
|
* to be revised.
|
|
|
|
*/
|
|
|
|
static PRUint64
|
|
|
|
DCacheHash(const char * key)
|
|
|
|
{
|
2009-03-27 20:58:06 +03:00
|
|
|
// initval 0x7416f295 was chosen randomly
|
|
|
|
return (PRUint64(nsDiskCache::Hash(key, 0)) << 32) | nsDiskCache::Hash(key, 0x7416f295);
|
2004-10-20 09:00:23 +04:00
|
|
|
}
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
/******************************************************************************
|
|
|
|
* nsOfflineCacheEvictionFunction
|
|
|
|
*/
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2010-08-24 05:06:23 +04:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsOfflineCacheEvictionFunction, mozIStorageFunction)
|
2007-05-15 00:09:20 +04:00
|
|
|
|
|
|
|
// helper function for directly exposing the same data file binding
|
|
|
|
// path algorithm used in nsOfflineCacheBinding::Create
|
|
|
|
static nsresult
|
2007-07-25 10:31:27 +04:00
|
|
|
GetCacheDataFile(nsIFile *cacheDir, const char *key,
|
2007-05-15 00:09:20 +04:00
|
|
|
int generation, nsCOMPtr<nsIFile> &file)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2007-05-15 00:09:20 +04:00
|
|
|
cacheDir->Clone(getter_AddRefs(file));
|
|
|
|
if (!file)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2007-07-25 10:31:27 +04:00
|
|
|
PRUint64 hash = DCacheHash(key);
|
2007-05-15 00:09:20 +04:00
|
|
|
|
|
|
|
PRUint32 dir1 = (PRUint32) (hash & 0x0F);
|
|
|
|
PRUint32 dir2 = (PRUint32)((hash & 0xF0) >> 4);
|
|
|
|
|
|
|
|
hash >>= 8;
|
|
|
|
|
|
|
|
file->AppendNative(nsPrintfCString("%X", dir1));
|
|
|
|
file->AppendNative(nsPrintfCString("%X", dir2));
|
|
|
|
|
|
|
|
char leaf[64];
|
|
|
|
PR_snprintf(leaf, sizeof(leaf), "%014llX-%X", hash, generation);
|
|
|
|
return file->AppendNative(nsDependentCString(leaf));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-07-08 01:14:56 +04:00
|
|
|
nsOfflineCacheEvictionFunction::OnFunctionCall(mozIStorageValueArray *values, nsIVariant **_retval)
|
2007-05-15 00:09:20 +04:00
|
|
|
{
|
2007-07-26 04:47:33 +04:00
|
|
|
LOG(("nsOfflineCacheEvictionFunction::OnFunctionCall\n"));
|
|
|
|
|
2007-07-08 01:14:56 +04:00
|
|
|
*_retval = nsnull;
|
2007-05-15 00:09:20 +04:00
|
|
|
|
|
|
|
PRUint32 numEntries;
|
|
|
|
nsresult rv = values->GetNumEntries(&numEntries);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-06-29 02:27:03 +04:00
|
|
|
NS_ASSERTION(numEntries == 3, "unexpected number of arguments");
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
PRUint32 valueLen;
|
2012-06-29 02:27:03 +04:00
|
|
|
const char *clientID = values->AsSharedUTF8String(0, &valueLen);
|
|
|
|
const char *key = values->AsSharedUTF8String(1, &valueLen);
|
|
|
|
nsCAutoString fullKey(clientID);
|
|
|
|
fullKey.AppendLiteral(":");
|
|
|
|
fullKey.Append(key);
|
|
|
|
int generation = values->AsInt32(2);
|
|
|
|
|
|
|
|
// If the key is currently locked, refuse to delete this row.
|
|
|
|
if (mDevice->IsLocked(fullKey)) {
|
|
|
|
NS_ADDREF(*_retval = new IntegerVariant(SQLITE_IGNORE));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-05-15 00:09:20 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
2007-07-25 10:31:27 +04:00
|
|
|
rv = GetCacheDataFile(mDevice->CacheDirectory(), key,
|
2007-05-15 00:09:20 +04:00
|
|
|
generation, file);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
2007-07-25 10:31:27 +04:00
|
|
|
LOG(("GetCacheDataFile [key=%s generation=%d] failed [rv=%x]!\n",
|
|
|
|
key, generation, rv));
|
2007-05-15 00:09:20 +04:00
|
|
|
return rv;
|
2004-10-20 09:00:23 +04:00
|
|
|
}
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2007-07-26 04:47:33 +04:00
|
|
|
mItems.AppendObject(file);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsOfflineCacheEvictionFunction::Apply()
|
|
|
|
{
|
|
|
|
LOG(("nsOfflineCacheEvictionFunction::Apply\n"));
|
|
|
|
|
2012-04-01 02:18:08 +04:00
|
|
|
for (PRInt32 i = 0; i < mItems.Count(); i++) {
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
nsCAutoString path;
|
|
|
|
mItems[i]->GetNativePath(path);
|
|
|
|
LOG((" removing %s\n", path.get()));
|
|
|
|
#endif
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2012-04-01 02:18:08 +04:00
|
|
|
mItems[i]->Remove(false);
|
2007-07-26 04:47:33 +04:00
|
|
|
}
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2007-07-26 04:47:33 +04:00
|
|
|
Reset();
|
2004-10-20 09:00:23 +04:00
|
|
|
}
|
|
|
|
|
2012-07-11 10:16:00 +04:00
|
|
|
class nsOfflineCacheDiscardCache : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsOfflineCacheDiscardCache(nsOfflineCacheDevice *device,
|
|
|
|
nsCString &group,
|
|
|
|
nsCString &clientID)
|
|
|
|
: mDevice(device)
|
|
|
|
, mGroup(group)
|
|
|
|
, mClientID(clientID)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
if (mDevice->IsActiveCache(mGroup, mClientID))
|
|
|
|
{
|
|
|
|
mDevice->DeactivateGroup(mGroup);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mDevice->EvictEntries(mClientID.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsOfflineCacheDevice> mDevice;
|
|
|
|
nsCString mGroup;
|
|
|
|
nsCString mClientID;
|
|
|
|
};
|
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
/******************************************************************************
|
2007-05-15 00:09:20 +04:00
|
|
|
* nsOfflineCacheDeviceInfo
|
2004-10-20 09:00:23 +04:00
|
|
|
*/
|
|
|
|
|
2012-06-06 07:18:25 +04:00
|
|
|
class nsOfflineCacheDeviceInfo MOZ_FINAL : public nsICacheDeviceInfo
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSICACHEDEVICEINFO
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDeviceInfo(nsOfflineCacheDevice* device)
|
2004-10-20 09:00:23 +04:00
|
|
|
: mDevice(device)
|
|
|
|
{}
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
private:
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDevice* mDevice;
|
2004-10-20 09:00:23 +04:00
|
|
|
};
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(nsOfflineCacheDeviceInfo, nsICacheDeviceInfo)
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDeviceInfo::GetDescription(char **aDescription)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2007-05-15 00:09:20 +04:00
|
|
|
*aDescription = NS_strdup("Offline cache device");
|
2004-10-20 09:00:23 +04:00
|
|
|
return *aDescription ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDeviceInfo::GetUsageReport(char ** usageReport)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
nsCAutoString buffer;
|
2010-07-03 00:56:09 +04:00
|
|
|
buffer.AssignLiteral(" <tr>\n"
|
|
|
|
" <th>Cache Directory:</th>\n"
|
|
|
|
" <td>");
|
2012-06-06 06:08:30 +04:00
|
|
|
nsIFile *cacheDir = mDevice->CacheDirectory();
|
2004-10-20 09:00:23 +04:00
|
|
|
if (!cacheDir)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsAutoString path;
|
|
|
|
nsresult rv = cacheDir->GetPath(path);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
AppendUTF16toUTF8(path, buffer);
|
|
|
|
else
|
|
|
|
buffer.AppendLiteral("directory unavailable");
|
2010-07-03 00:56:09 +04:00
|
|
|
|
|
|
|
buffer.AppendLiteral("</td>\n"
|
|
|
|
" </tr>\n");
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
*usageReport = ToNewCString(buffer);
|
|
|
|
if (!*usageReport)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDeviceInfo::GetEntryCount(PRUint32 *aEntryCount)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
*aEntryCount = mDevice->EntryCount();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDeviceInfo::GetTotalSize(PRUint32 *aTotalSize)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
*aTotalSize = mDevice->CacheSize();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDeviceInfo::GetMaximumSize(PRUint32 *aMaximumSize)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
*aMaximumSize = mDevice->CacheCapacity();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
2007-05-15 00:09:20 +04:00
|
|
|
* nsOfflineCacheBinding
|
2004-10-20 09:00:23 +04:00
|
|
|
*/
|
|
|
|
|
2012-06-06 07:18:25 +04:00
|
|
|
class nsOfflineCacheBinding MOZ_FINAL : public nsISupports
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
static nsOfflineCacheBinding *
|
2007-07-25 10:31:27 +04:00
|
|
|
Create(nsIFile *cacheDir, const nsCString *key, int generation);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2012-07-17 04:38:47 +04:00
|
|
|
enum { FLAG_NEW_ENTRY = 1 };
|
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
nsCOMPtr<nsIFile> mDataFile;
|
|
|
|
int mGeneration;
|
2012-07-17 04:38:47 +04:00
|
|
|
int mFlags;
|
|
|
|
|
|
|
|
bool IsNewEntry() { return mFlags & FLAG_NEW_ENTRY; }
|
|
|
|
void MarkNewEntry() { mFlags |= FLAG_NEW_ENTRY; }
|
|
|
|
void ClearNewEntry() { mFlags &= ~FLAG_NEW_ENTRY; }
|
2004-10-20 09:00:23 +04:00
|
|
|
};
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS0(nsOfflineCacheBinding)
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheBinding *
|
|
|
|
nsOfflineCacheBinding::Create(nsIFile *cacheDir,
|
2007-07-25 10:31:27 +04:00
|
|
|
const nsCString *fullKey,
|
2007-05-15 00:09:20 +04:00
|
|
|
int generation)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
cacheDir->Clone(getter_AddRefs(file));
|
|
|
|
if (!file)
|
|
|
|
return nsnull;
|
|
|
|
|
2007-07-25 10:31:27 +04:00
|
|
|
nsCAutoString keyBuf;
|
|
|
|
const char *cid, *key;
|
|
|
|
if (!DecomposeCacheEntryKey(fullKey, &cid, &key, keyBuf))
|
|
|
|
return nsnull;
|
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
PRUint64 hash = DCacheHash(key);
|
|
|
|
|
|
|
|
PRUint32 dir1 = (PRUint32) (hash & 0x0F);
|
|
|
|
PRUint32 dir2 = (PRUint32)((hash & 0xF0) >> 4);
|
|
|
|
|
|
|
|
hash >>= 8;
|
|
|
|
|
|
|
|
// XXX we might want to create these directories up-front
|
|
|
|
|
|
|
|
file->AppendNative(nsPrintfCString("%X", dir1));
|
|
|
|
file->Create(nsIFile::DIRECTORY_TYPE, 00700);
|
|
|
|
|
|
|
|
file->AppendNative(nsPrintfCString("%X", dir2));
|
|
|
|
file->Create(nsIFile::DIRECTORY_TYPE, 00700);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
char leaf[64];
|
|
|
|
|
|
|
|
if (generation == -1)
|
|
|
|
{
|
|
|
|
file->AppendNative(NS_LITERAL_CSTRING("placeholder"));
|
|
|
|
|
|
|
|
for (generation = 0; ; ++generation)
|
|
|
|
{
|
|
|
|
PR_snprintf(leaf, sizeof(leaf), "%014llX-%X", hash, generation);
|
|
|
|
|
|
|
|
rv = file->SetNativeLeafName(nsDependentCString(leaf));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return nsnull;
|
|
|
|
rv = file->Create(nsIFile::NORMAL_FILE_TYPE, 00600);
|
|
|
|
if (NS_FAILED(rv) && rv != NS_ERROR_FILE_ALREADY_EXISTS)
|
|
|
|
return nsnull;
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PR_snprintf(leaf, sizeof(leaf), "%014llX-%X", hash, generation);
|
|
|
|
rv = file->AppendNative(nsDependentCString(leaf));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheBinding *binding = new nsOfflineCacheBinding;
|
2004-10-20 09:00:23 +04:00
|
|
|
if (!binding)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
binding->mDataFile.swap(file);
|
|
|
|
binding->mGeneration = generation;
|
2012-07-17 04:38:47 +04:00
|
|
|
binding->mFlags = 0;
|
2004-10-20 09:00:23 +04:00
|
|
|
return binding;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
2007-05-15 00:09:20 +04:00
|
|
|
* nsOfflineCacheRecord
|
2004-10-20 09:00:23 +04:00
|
|
|
*/
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
struct nsOfflineCacheRecord
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
const char *clientID;
|
|
|
|
const char *key;
|
|
|
|
const PRUint8 *metaData;
|
|
|
|
PRUint32 metaDataLen;
|
|
|
|
PRInt32 generation;
|
|
|
|
PRInt32 dataSize;
|
|
|
|
PRInt32 fetchCount;
|
|
|
|
PRInt64 lastFetched;
|
|
|
|
PRInt64 lastModified;
|
|
|
|
PRInt64 expirationTime;
|
|
|
|
};
|
|
|
|
|
|
|
|
static nsCacheEntry *
|
2007-05-15 00:09:20 +04:00
|
|
|
CreateCacheEntry(nsOfflineCacheDevice *device,
|
2004-10-20 09:00:23 +04:00
|
|
|
const nsCString *fullKey,
|
2007-05-15 00:09:20 +04:00
|
|
|
const nsOfflineCacheRecord &rec)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
nsCacheEntry *entry;
|
2012-06-29 02:27:03 +04:00
|
|
|
|
|
|
|
if (device->IsLocked(*fullKey)) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
nsresult rv = nsCacheEntry::Create(fullKey->get(), // XXX enable sharing
|
|
|
|
nsICache::STREAM_BASED,
|
2007-05-15 00:09:20 +04:00
|
|
|
nsICache::STORE_OFFLINE,
|
2004-10-20 09:00:23 +04:00
|
|
|
device, &entry);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
entry->SetFetchCount((PRUint32) rec.fetchCount);
|
|
|
|
entry->SetLastFetched(SecondsFromPRTime(rec.lastFetched));
|
|
|
|
entry->SetLastModified(SecondsFromPRTime(rec.lastModified));
|
|
|
|
entry->SetExpirationTime(SecondsFromPRTime(rec.expirationTime));
|
|
|
|
entry->SetDataSize((PRUint32) rec.dataSize);
|
|
|
|
|
|
|
|
entry->UnflattenMetaData((const char *) rec.metaData, rec.metaDataLen);
|
|
|
|
|
|
|
|
// create a binding object for this entry
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheBinding *binding =
|
|
|
|
nsOfflineCacheBinding::Create(device->CacheDirectory(),
|
2007-07-25 10:31:27 +04:00
|
|
|
fullKey,
|
2007-05-15 00:09:20 +04:00
|
|
|
rec.generation);
|
2004-10-20 09:00:23 +04:00
|
|
|
if (!binding)
|
|
|
|
{
|
|
|
|
delete entry;
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
entry->SetData(binding);
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
2007-05-15 00:09:20 +04:00
|
|
|
* nsOfflineCacheEntryInfo
|
2004-10-20 09:00:23 +04:00
|
|
|
*/
|
|
|
|
|
2012-06-06 07:18:25 +04:00
|
|
|
class nsOfflineCacheEntryInfo MOZ_FINAL : public nsICacheEntryInfo
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSICACHEENTRYINFO
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheRecord *mRec;
|
2004-10-20 09:00:23 +04:00
|
|
|
};
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(nsOfflineCacheEntryInfo, nsICacheEntryInfo)
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheEntryInfo::GetClientID(char **result)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2007-05-15 00:09:20 +04:00
|
|
|
*result = NS_strdup(mRec->clientID);
|
2004-10-20 09:00:23 +04:00
|
|
|
return *result ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheEntryInfo::GetDeviceID(char ** deviceID)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2007-05-15 00:09:20 +04:00
|
|
|
*deviceID = NS_strdup(OFFLINE_CACHE_DEVICE_ID);
|
2004-10-20 09:00:23 +04:00
|
|
|
return *deviceID ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheEntryInfo::GetKey(nsACString &clientKey)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2005-01-13 06:25:28 +03:00
|
|
|
clientKey.Assign(mRec->key);
|
|
|
|
return NS_OK;
|
2004-10-20 09:00:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheEntryInfo::GetFetchCount(PRInt32 *aFetchCount)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
*aFetchCount = mRec->fetchCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheEntryInfo::GetLastFetched(PRUint32 *aLastFetched)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
*aLastFetched = SecondsFromPRTime(mRec->lastFetched);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheEntryInfo::GetLastModified(PRUint32 *aLastModified)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
*aLastModified = SecondsFromPRTime(mRec->lastModified);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheEntryInfo::GetExpirationTime(PRUint32 *aExpirationTime)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
*aExpirationTime = SecondsFromPRTime(mRec->expirationTime);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsOfflineCacheEntryInfo::IsStreamBased(bool *aStreamBased)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
*aStreamBased = true;
|
2004-10-20 09:00:23 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOfflineCacheEntryInfo::GetDataSize(PRUint32 *aDataSize)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
*aDataSize = mRec->dataSize;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-30 22:34:23 +04:00
|
|
|
/******************************************************************************
|
|
|
|
* nsApplicationCacheNamespace
|
|
|
|
*/
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsApplicationCacheNamespace, nsIApplicationCacheNamespace)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsApplicationCacheNamespace::Init(PRUint32 itemType,
|
|
|
|
const nsACString &namespaceSpec,
|
|
|
|
const nsACString &data)
|
|
|
|
{
|
|
|
|
mItemType = itemType;
|
|
|
|
mNamespaceSpec = namespaceSpec;
|
|
|
|
mData = data;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsApplicationCacheNamespace::GetItemType(PRUint32 *out)
|
|
|
|
{
|
|
|
|
*out = mItemType;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsApplicationCacheNamespace::GetNamespaceSpec(nsACString &out)
|
|
|
|
{
|
|
|
|
out = mNamespaceSpec;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsApplicationCacheNamespace::GetData(nsACString &out)
|
|
|
|
{
|
|
|
|
out = mData;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
/******************************************************************************
|
|
|
|
* nsApplicationCache
|
|
|
|
*/
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS2(nsApplicationCache,
|
|
|
|
nsIApplicationCache,
|
|
|
|
nsISupportsWeakReference)
|
|
|
|
|
2010-10-20 21:12:32 +04:00
|
|
|
nsApplicationCache::nsApplicationCache()
|
|
|
|
: mDevice(nsnull)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mValid(true)
|
2010-10-20 21:12:32 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
nsApplicationCache::nsApplicationCache(nsOfflineCacheDevice *device,
|
|
|
|
const nsACString &group,
|
|
|
|
const nsACString &clientID)
|
|
|
|
: mDevice(device)
|
|
|
|
, mGroup(group)
|
|
|
|
, mClientID(clientID)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mValid(true)
|
2008-08-27 03:09:02 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsApplicationCache::~nsApplicationCache()
|
|
|
|
{
|
2010-10-20 21:12:32 +04:00
|
|
|
if (!mDevice)
|
|
|
|
return;
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
mDevice->mCaches.Remove(mClientID);
|
|
|
|
|
|
|
|
// If this isn't an active cache anymore, it can be destroyed.
|
|
|
|
if (mValid && !mDevice->IsActiveCache(mGroup, mClientID))
|
|
|
|
Discard();
|
|
|
|
}
|
|
|
|
|
2010-10-20 21:12:32 +04:00
|
|
|
void
|
|
|
|
nsApplicationCache::MarkInvalid()
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mValid = false;
|
2010-10-20 21:12:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsApplicationCache::InitAsHandle(const nsACString &groupId,
|
|
|
|
const nsACString &clientId)
|
|
|
|
{
|
|
|
|
NS_ENSURE_FALSE(mDevice, NS_ERROR_ALREADY_INITIALIZED);
|
|
|
|
NS_ENSURE_TRUE(mGroup.IsEmpty(), NS_ERROR_ALREADY_INITIALIZED);
|
|
|
|
|
|
|
|
mGroup = groupId;
|
|
|
|
mClientID = clientId;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsApplicationCache::GetGroupID(nsACString &out)
|
|
|
|
{
|
|
|
|
out = mGroup;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsApplicationCache::GetClientID(nsACString &out)
|
|
|
|
{
|
|
|
|
out = mClientID;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-04 18:12:24 +04:00
|
|
|
NS_IMETHODIMP
|
2012-07-11 22:20:17 +04:00
|
|
|
nsApplicationCache::GetProfileDirectory(nsIFile **out)
|
2012-06-04 18:12:24 +04:00
|
|
|
{
|
|
|
|
if (mDevice->BaseDirectory())
|
|
|
|
NS_ADDREF(*out = mDevice->BaseDirectory());
|
|
|
|
else
|
|
|
|
*out = nsnull;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsApplicationCache::GetActive(bool *out)
|
2008-08-27 03:09:02 +04:00
|
|
|
{
|
2010-10-20 21:12:32 +04:00
|
|
|
NS_ENSURE_TRUE(mDevice, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
*out = mDevice->IsActiveCache(mGroup, mClientID);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsApplicationCache::Activate()
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mValid, NS_ERROR_NOT_AVAILABLE);
|
2010-10-20 21:12:32 +04:00
|
|
|
NS_ENSURE_TRUE(mDevice, NS_ERROR_NOT_AVAILABLE);
|
2008-08-27 03:09:02 +04:00
|
|
|
|
|
|
|
mDevice->ActivateCache(mGroup, mClientID);
|
2012-07-11 22:20:17 +04:00
|
|
|
|
|
|
|
if (mDevice->AutoShutdown(this))
|
|
|
|
mDevice = nsnull;
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsApplicationCache::Discard()
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mValid, NS_ERROR_NOT_AVAILABLE);
|
2010-10-20 21:12:32 +04:00
|
|
|
NS_ENSURE_TRUE(mDevice, NS_ERROR_NOT_AVAILABLE);
|
2008-08-27 03:09:02 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mValid = false;
|
2008-08-27 03:09:02 +04:00
|
|
|
|
2012-07-11 10:16:00 +04:00
|
|
|
nsRefPtr<nsIRunnable> ev =
|
|
|
|
new nsOfflineCacheDiscardCache(mDevice, mGroup, mClientID);
|
|
|
|
nsresult rv = nsCacheService::DispatchToCacheIOThread(ev);
|
|
|
|
return rv;
|
2008-08-27 03:09:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsApplicationCache::MarkEntry(const nsACString &key,
|
|
|
|
PRUint32 typeBits)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mValid, NS_ERROR_NOT_AVAILABLE);
|
2010-10-20 21:12:32 +04:00
|
|
|
NS_ENSURE_TRUE(mDevice, NS_ERROR_NOT_AVAILABLE);
|
2008-08-27 03:09:02 +04:00
|
|
|
|
|
|
|
return mDevice->MarkEntry(mClientID, key, typeBits);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsApplicationCache::UnmarkEntry(const nsACString &key,
|
|
|
|
PRUint32 typeBits)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mValid, NS_ERROR_NOT_AVAILABLE);
|
2010-10-20 21:12:32 +04:00
|
|
|
NS_ENSURE_TRUE(mDevice, NS_ERROR_NOT_AVAILABLE);
|
2008-08-27 03:09:02 +04:00
|
|
|
|
|
|
|
return mDevice->UnmarkEntry(mClientID, key, typeBits);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsApplicationCache::GetTypes(const nsACString &key,
|
|
|
|
PRUint32 *typeBits)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mValid, NS_ERROR_NOT_AVAILABLE);
|
2010-10-20 21:12:32 +04:00
|
|
|
NS_ENSURE_TRUE(mDevice, NS_ERROR_NOT_AVAILABLE);
|
2008-08-27 03:09:02 +04:00
|
|
|
|
|
|
|
return mDevice->GetTypes(mClientID, key, typeBits);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsApplicationCache::GatherEntries(PRUint32 typeBits,
|
|
|
|
PRUint32 * count,
|
|
|
|
char *** keys)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mValid, NS_ERROR_NOT_AVAILABLE);
|
2010-10-20 21:12:32 +04:00
|
|
|
NS_ENSURE_TRUE(mDevice, NS_ERROR_NOT_AVAILABLE);
|
2008-08-27 03:09:02 +04:00
|
|
|
|
|
|
|
return mDevice->GatherEntries(mClientID, typeBits, count, keys);
|
|
|
|
}
|
|
|
|
|
2008-09-30 22:34:23 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsApplicationCache::AddNamespaces(nsIArray *namespaces)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mValid, NS_ERROR_NOT_AVAILABLE);
|
2010-10-20 21:12:32 +04:00
|
|
|
NS_ENSURE_TRUE(mDevice, NS_ERROR_NOT_AVAILABLE);
|
2008-09-30 22:34:23 +04:00
|
|
|
|
|
|
|
if (!namespaces)
|
|
|
|
return NS_OK;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mozStorageTransaction transaction(mDevice->mDB, false);
|
2008-09-30 22:34:23 +04:00
|
|
|
|
|
|
|
PRUint32 length;
|
|
|
|
nsresult rv = namespaces->GetLength(&length);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < length; i++) {
|
|
|
|
nsCOMPtr<nsIApplicationCacheNamespace> ns =
|
|
|
|
do_QueryElementAt(namespaces, i);
|
|
|
|
if (ns) {
|
|
|
|
rv = mDevice->AddNamespace(mClientID, ns);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = transaction.Commit();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsApplicationCache::GetMatchingNamespace(const nsACString &key,
|
|
|
|
nsIApplicationCacheNamespace **out)
|
|
|
|
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mValid, NS_ERROR_NOT_AVAILABLE);
|
2010-10-20 21:12:32 +04:00
|
|
|
NS_ENSURE_TRUE(mDevice, NS_ERROR_NOT_AVAILABLE);
|
2008-09-30 22:34:23 +04:00
|
|
|
|
|
|
|
return mDevice->GetMatchingNamespace(mClientID, key, out);
|
|
|
|
}
|
|
|
|
|
2009-02-22 22:09:10 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsApplicationCache::GetUsage(PRUint32 *usage)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mValid, NS_ERROR_NOT_AVAILABLE);
|
2010-10-20 21:12:32 +04:00
|
|
|
NS_ENSURE_TRUE(mDevice, NS_ERROR_NOT_AVAILABLE);
|
2009-02-22 22:09:10 +03:00
|
|
|
|
|
|
|
return mDevice->GetUsage(mClientID, usage);
|
|
|
|
}
|
|
|
|
|
2010-08-24 05:06:23 +04:00
|
|
|
/******************************************************************************
|
|
|
|
* nsCloseDBEvent
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
class nsCloseDBEvent : public nsRunnable {
|
|
|
|
public:
|
|
|
|
nsCloseDBEvent(mozIStorageConnection *aDB)
|
|
|
|
{
|
|
|
|
mDB = aDB;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
mDB->Close();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~nsCloseDBEvent() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<mozIStorageConnection> mDB;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
/******************************************************************************
|
2007-05-15 00:09:20 +04:00
|
|
|
* nsOfflineCacheDevice
|
2004-10-20 09:00:23 +04:00
|
|
|
*/
|
|
|
|
|
2012-04-30 21:57:09 +04:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS0(nsOfflineCacheDevice)
|
2008-08-27 03:09:02 +04:00
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDevice::nsOfflineCacheDevice()
|
2004-10-20 09:00:23 +04:00
|
|
|
: mDB(nsnull)
|
|
|
|
, mCacheCapacity(0)
|
|
|
|
, mDeltaCounter(0)
|
2012-07-11 22:20:17 +04:00
|
|
|
, mAutoShutdown(false)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-08-28 05:15:32 +04:00
|
|
|
/* static */
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2008-08-28 05:15:32 +04:00
|
|
|
nsOfflineCacheDevice::GetStrictFileOriginPolicy()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool retval;
|
2008-08-28 05:15:32 +04:00
|
|
|
if (prefs && NS_SUCCEEDED(prefs->GetBoolPref("security.fileuri.strict_origin_policy", &retval)))
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
// As default value use true (be more strict)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-08-28 05:15:32 +04:00
|
|
|
}
|
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
PRUint32
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDevice::CacheSize()
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2004-10-28 02:19:32 +04:00
|
|
|
AutoResetStatement statement(mStatement_CacheSize);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasRows;
|
2004-10-28 02:19:32 +04:00
|
|
|
nsresult rv = statement->ExecuteStep(&hasRows);
|
2004-10-20 09:00:23 +04:00
|
|
|
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && hasRows, 0);
|
|
|
|
|
|
|
|
return (PRUint32) statement->AsInt32(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDevice::EntryCount()
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2004-10-28 02:19:32 +04:00
|
|
|
AutoResetStatement statement(mStatement_EntryCount);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasRows;
|
2004-10-28 02:19:32 +04:00
|
|
|
nsresult rv = statement->ExecuteStep(&hasRows);
|
2004-10-20 09:00:23 +04:00
|
|
|
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && hasRows, 0);
|
|
|
|
|
|
|
|
return (PRUint32) statement->AsInt32(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDevice::UpdateEntry(nsCacheEntry *entry)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
// Decompose the key into "ClientID" and "Key"
|
|
|
|
nsCAutoString keyBuf;
|
|
|
|
const char *cid, *key;
|
2012-07-17 04:38:47 +04:00
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
if (!DecomposeCacheEntryKey(entry->Key(), &cid, &key, keyBuf))
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
nsCString metaDataBuf;
|
|
|
|
PRUint32 mdSize = entry->MetaDataSize();
|
2006-06-22 23:07:30 +04:00
|
|
|
if (!EnsureStringLength(metaDataBuf, mdSize))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2004-10-20 09:00:23 +04:00
|
|
|
char *md = metaDataBuf.BeginWriting();
|
|
|
|
entry->FlattenMetaData(md, mdSize);
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheRecord rec;
|
2004-10-20 09:00:23 +04:00
|
|
|
rec.metaData = (const PRUint8 *) md;
|
|
|
|
rec.metaDataLen = mdSize;
|
|
|
|
rec.dataSize = entry->DataSize();
|
|
|
|
rec.fetchCount = entry->FetchCount();
|
|
|
|
rec.lastFetched = PRTimeFromSeconds(entry->LastFetched());
|
|
|
|
rec.lastModified = PRTimeFromSeconds(entry->LastModified());
|
|
|
|
rec.expirationTime = PRTimeFromSeconds(entry->ExpirationTime());
|
|
|
|
|
2004-10-28 02:19:32 +04:00
|
|
|
AutoResetStatement statement(mStatement_UpdateEntry);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2004-10-28 02:19:32 +04:00
|
|
|
nsresult rv;
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = statement->BindBlobByIndex(0, rec.metaData, rec.metaDataLen);
|
2012-06-29 02:26:55 +04:00
|
|
|
rv |= statement->BindInt32ByIndex(1, rec.dataSize);
|
|
|
|
rv |= statement->BindInt32ByIndex(2, rec.fetchCount);
|
|
|
|
rv |= statement->BindInt64ByIndex(3, rec.lastFetched);
|
|
|
|
rv |= statement->BindInt64ByIndex(4, rec.lastModified);
|
|
|
|
rv |= statement->BindInt64ByIndex(5, rec.expirationTime);
|
|
|
|
rv |= statement->BindUTF8StringByIndex(6, nsDependentCString(cid));
|
|
|
|
rv |= statement->BindUTF8StringByIndex(7, nsDependentCString(key));
|
2004-10-20 09:00:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasRows;
|
2004-10-20 09:00:23 +04:00
|
|
|
rv = statement->ExecuteStep(&hasRows);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_ASSERTION(!hasRows, "UPDATE should not result in output");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDevice::UpdateEntrySize(nsCacheEntry *entry, PRUint32 newSize)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
// Decompose the key into "ClientID" and "Key"
|
|
|
|
nsCAutoString keyBuf;
|
|
|
|
const char *cid, *key;
|
|
|
|
if (!DecomposeCacheEntryKey(entry->Key(), &cid, &key, keyBuf))
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2004-10-28 02:19:32 +04:00
|
|
|
AutoResetStatement statement(mStatement_UpdateEntrySize);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2004-10-28 02:19:32 +04:00
|
|
|
nsresult rv;
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = statement->BindInt32ByIndex(0, newSize);
|
|
|
|
rv |= statement->BindUTF8StringByIndex(1, nsDependentCString(cid));
|
|
|
|
rv |= statement->BindUTF8StringByIndex(2, nsDependentCString(key));
|
2004-10-20 09:00:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasRows;
|
2004-10-20 09:00:23 +04:00
|
|
|
rv = statement->ExecuteStep(&hasRows);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_ASSERTION(!hasRows, "UPDATE should not result in output");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsOfflineCacheDevice::DeleteEntry(nsCacheEntry *entry, bool deleteData)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
if (deleteData)
|
|
|
|
{
|
|
|
|
nsresult rv = DeleteData(entry);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decompose the key into "ClientID" and "Key"
|
|
|
|
nsCAutoString keyBuf;
|
|
|
|
const char *cid, *key;
|
|
|
|
if (!DecomposeCacheEntryKey(entry->Key(), &cid, &key, keyBuf))
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2004-10-28 02:19:32 +04:00
|
|
|
AutoResetStatement statement(mStatement_DeleteEntry);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2004-10-28 02:19:32 +04:00
|
|
|
nsresult rv;
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = statement->BindUTF8StringByIndex(0, nsDependentCString(cid));
|
|
|
|
rv |= statement->BindUTF8StringByIndex(1, nsDependentCString(key));
|
2004-10-20 09:00:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasRows;
|
2004-10-20 09:00:23 +04:00
|
|
|
rv = statement->ExecuteStep(&hasRows);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_ASSERTION(!hasRows, "DELETE should not result in output");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDevice::DeleteData(nsCacheEntry *entry)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheBinding *binding = (nsOfflineCacheBinding *) entry->Data();
|
2004-10-20 09:00:23 +04:00
|
|
|
NS_ENSURE_STATE(binding);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return binding->mDataFile->Remove(false);
|
2004-10-20 09:00:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nsCacheDevice implementation
|
|
|
|
*/
|
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
// This struct is local to nsOfflineCacheDevice::Init, but ISO C++98 doesn't
|
|
|
|
// allow a template (mozilla::ArrayLength) to be instantiated based on a local
|
|
|
|
// type. Boo-urns!
|
|
|
|
struct StatementSql {
|
|
|
|
nsCOMPtr<mozIStorageStatement> &statement;
|
|
|
|
const char *sql;
|
|
|
|
StatementSql (nsCOMPtr<mozIStorageStatement> &aStatement, const char *aSql):
|
|
|
|
statement (aStatement), sql (aSql) {}
|
|
|
|
};
|
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
nsresult
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDevice::Init()
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2010-05-20 03:22:19 +04:00
|
|
|
NS_TIME_FUNCTION;
|
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
NS_ENSURE_TRUE(!mDB, NS_ERROR_ALREADY_INITIALIZED);
|
|
|
|
|
|
|
|
// SetCacheParentDirectory must have been called
|
|
|
|
NS_ENSURE_TRUE(mCacheDirectory, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
// make sure the cache directory exists
|
|
|
|
nsresult rv = EnsureDir(mCacheDirectory);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// build path to index file
|
|
|
|
nsCOMPtr<nsIFile> indexFile;
|
|
|
|
rv = mCacheDirectory->Clone(getter_AddRefs(indexFile));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-05-15 00:09:20 +04:00
|
|
|
rv = indexFile->AppendNative(NS_LITERAL_CSTRING("index.sqlite"));
|
2004-10-20 09:00:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<mozIStorageService> ss =
|
2007-05-15 00:09:20 +04:00
|
|
|
do_GetService("@mozilla.org/storage/service;1", &rv);
|
2004-10-20 09:00:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = ss->OpenDatabase(indexFile, getter_AddRefs(mDB));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-08-24 05:06:23 +04:00
|
|
|
mInitThread = do_GetCurrentThread();
|
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
mDB->ExecuteSimpleSQL(NS_LITERAL_CSTRING("PRAGMA synchronous = OFF;"));
|
|
|
|
|
|
|
|
// XXX ... other initialization steps
|
|
|
|
|
|
|
|
// XXX in the future we may wish to verify the schema for moz_cache
|
|
|
|
// perhaps using "PRAGMA table_info" ?
|
|
|
|
|
|
|
|
// build the table
|
|
|
|
//
|
|
|
|
// "Generation" is the data file generation number.
|
|
|
|
//
|
2008-08-27 03:09:02 +04:00
|
|
|
rv = mDB->ExecuteSimpleSQL(
|
|
|
|
NS_LITERAL_CSTRING("CREATE TABLE IF NOT EXISTS moz_cache (\n"
|
2004-10-20 09:00:23 +04:00
|
|
|
" ClientID TEXT,\n"
|
|
|
|
" Key TEXT,\n"
|
|
|
|
" MetaData BLOB,\n"
|
|
|
|
" Generation INTEGER,\n"
|
|
|
|
" DataSize INTEGER,\n"
|
|
|
|
" FetchCount INTEGER,\n"
|
|
|
|
" LastFetched INTEGER,\n"
|
|
|
|
" LastModified INTEGER,\n"
|
2008-08-27 03:09:02 +04:00
|
|
|
" ExpirationTime INTEGER,\n"
|
|
|
|
" ItemType INTEGER DEFAULT 0\n"
|
2004-10-20 09:00:23 +04:00
|
|
|
");\n"));
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
// Databases from 1.9.0 don't have the ItemType column. Add the column
|
|
|
|
// here, but don't worry about failures (the column probably already exists)
|
2007-05-15 00:09:20 +04:00
|
|
|
mDB->ExecuteSimpleSQL(
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_LITERAL_CSTRING("ALTER TABLE moz_cache ADD ItemType INTEGER DEFAULT 0"));
|
|
|
|
|
|
|
|
// Create the table for storing cache groups. All actions on
|
|
|
|
// moz_cache_groups use the GroupID, so use it as the primary key.
|
|
|
|
rv = mDB->ExecuteSimpleSQL(
|
|
|
|
NS_LITERAL_CSTRING("CREATE TABLE IF NOT EXISTS moz_cache_groups (\n"
|
|
|
|
" GroupID TEXT PRIMARY KEY,\n"
|
|
|
|
" ActiveClientID TEXT\n"
|
2007-05-15 00:09:20 +04:00
|
|
|
");\n"));
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2008-09-30 22:34:23 +04:00
|
|
|
mDB->ExecuteSimpleSQL(
|
|
|
|
NS_LITERAL_CSTRING("ALTER TABLE moz_cache_groups "
|
|
|
|
"ADD ActivateTimeStamp INTEGER DEFAULT 0"));
|
|
|
|
|
|
|
|
// ClientID: clientID joining moz_cache and moz_cache_namespaces
|
|
|
|
// tables.
|
|
|
|
// Data: Data associated with this namespace (e.g. a fallback URI
|
|
|
|
// for fallback entries).
|
|
|
|
// ItemType: the type of namespace.
|
|
|
|
rv = mDB->ExecuteSimpleSQL(
|
|
|
|
NS_LITERAL_CSTRING("CREATE TABLE IF NOT EXISTS"
|
|
|
|
" moz_cache_namespaces (\n"
|
|
|
|
" ClientID TEXT,\n"
|
|
|
|
" NameSpace TEXT,\n"
|
|
|
|
" Data TEXT,\n"
|
|
|
|
" ItemType INTEGER\n"
|
|
|
|
");\n"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
// Databases from 1.9.0 have a moz_cache_index that should be dropped
|
|
|
|
rv = mDB->ExecuteSimpleSQL(
|
|
|
|
NS_LITERAL_CSTRING("DROP INDEX IF EXISTS moz_cache_index"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Key/ClientID pairs should be unique in the database. All queries
|
|
|
|
// against moz_cache use the Key (which is also the most unique), so
|
|
|
|
// use it as the primary key for this index.
|
|
|
|
rv = mDB->ExecuteSimpleSQL(
|
|
|
|
NS_LITERAL_CSTRING("CREATE UNIQUE INDEX IF NOT EXISTS "
|
|
|
|
" moz_cache_key_clientid_index"
|
|
|
|
" ON moz_cache (Key, ClientID);"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2008-09-30 22:34:23 +04:00
|
|
|
// Used for ClientID lookups and to keep ClientID/NameSpace pairs unique.
|
|
|
|
rv = mDB->ExecuteSimpleSQL(
|
|
|
|
NS_LITERAL_CSTRING("CREATE UNIQUE INDEX IF NOT EXISTS"
|
|
|
|
" moz_cache_namespaces_clientid_index"
|
|
|
|
" ON moz_cache_namespaces (ClientID, NameSpace);"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Used for namespace lookups.
|
|
|
|
rv = mDB->ExecuteSimpleSQL(
|
|
|
|
NS_LITERAL_CSTRING("CREATE INDEX IF NOT EXISTS"
|
|
|
|
" moz_cache_namespaces_namespace_index"
|
|
|
|
" ON moz_cache_namespaces (NameSpace);"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
|
2007-07-26 04:47:33 +04:00
|
|
|
mEvictionFunction = new nsOfflineCacheEvictionFunction(this);
|
|
|
|
if (!mEvictionFunction) return NS_ERROR_OUT_OF_MEMORY;
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2012-06-29 02:27:03 +04:00
|
|
|
rv = mDB->CreateFunction(NS_LITERAL_CSTRING("cache_eviction_observer"), 3, mEvictionFunction);
|
2007-05-15 00:09:20 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2004-10-28 02:19:32 +04:00
|
|
|
// create all (most) of our statements up front
|
2011-10-11 09:50:08 +04:00
|
|
|
StatementSql prepared[] = {
|
2007-05-15 11:53:42 +04:00
|
|
|
StatementSql ( mStatement_CacheSize, "SELECT Sum(DataSize) from moz_cache;" ),
|
2009-02-22 22:09:10 +03:00
|
|
|
StatementSql ( mStatement_ApplicationCacheSize, "SELECT Sum(DataSize) from moz_cache WHERE ClientID = ?;" ),
|
2007-05-15 11:53:42 +04:00
|
|
|
StatementSql ( mStatement_EntryCount, "SELECT count(*) from moz_cache;" ),
|
2012-06-29 02:26:55 +04:00
|
|
|
StatementSql ( mStatement_UpdateEntry, "UPDATE moz_cache SET MetaData = ?, DataSize = ?, FetchCount = ?, LastFetched = ?, LastModified = ?, ExpirationTime = ? WHERE ClientID = ? AND Key = ?;" ),
|
2007-05-15 11:53:42 +04:00
|
|
|
StatementSql ( mStatement_UpdateEntrySize, "UPDATE moz_cache SET DataSize = ? WHERE ClientID = ? AND Key = ?;" ),
|
|
|
|
StatementSql ( mStatement_DeleteEntry, "DELETE FROM moz_cache WHERE ClientID = ? AND Key = ?;" ),
|
2012-06-29 02:26:55 +04:00
|
|
|
StatementSql ( mStatement_FindEntry, "SELECT MetaData, Generation, DataSize, FetchCount, LastFetched, LastModified, ExpirationTime, ItemType FROM moz_cache WHERE ClientID = ? AND Key = ?;" ),
|
|
|
|
StatementSql ( mStatement_BindEntry, "INSERT INTO moz_cache (ClientID, Key, MetaData, Generation, DataSize, FetchCount, LastFetched, LastModified, ExpirationTime) VALUES(?,?,?,?,?,?,?,?,?);" ),
|
2008-08-27 03:09:02 +04:00
|
|
|
|
|
|
|
StatementSql ( mStatement_MarkEntry, "UPDATE moz_cache SET ItemType = (ItemType | ?) WHERE ClientID = ? AND Key = ?;" ),
|
|
|
|
StatementSql ( mStatement_UnmarkEntry, "UPDATE moz_cache SET ItemType = (ItemType & ~?) WHERE ClientID = ? AND Key = ?;" ),
|
|
|
|
StatementSql ( mStatement_GetTypes, "SELECT ItemType FROM moz_cache WHERE ClientID = ? AND Key = ?;"),
|
|
|
|
StatementSql ( mStatement_CleanupUnmarked, "DELETE FROM moz_cache WHERE ClientID = ? AND Key = ? AND ItemType = 0;" ),
|
|
|
|
StatementSql ( mStatement_GatherEntries, "SELECT Key FROM moz_cache WHERE ClientID = ? AND (ItemType & ?) > 0;" ),
|
|
|
|
|
2008-09-30 22:34:23 +04:00
|
|
|
StatementSql ( mStatement_ActivateClient, "INSERT OR REPLACE INTO moz_cache_groups (GroupID, ActiveClientID, ActivateTimeStamp) VALUES (?, ?, ?);" ),
|
2008-08-27 03:09:02 +04:00
|
|
|
StatementSql ( mStatement_DeactivateGroup, "DELETE FROM moz_cache_groups WHERE GroupID = ?;" ),
|
2008-09-30 22:34:23 +04:00
|
|
|
StatementSql ( mStatement_FindClient, "SELECT ClientID, ItemType FROM moz_cache WHERE Key = ? ORDER BY LastFetched DESC, LastModified DESC;" ),
|
|
|
|
|
|
|
|
// Search for namespaces that match the URI. Use the <= operator
|
|
|
|
// to ensure that we use the index on moz_cache_namespaces.
|
|
|
|
StatementSql ( mStatement_FindClientByNamespace, "SELECT ns.ClientID, ns.ItemType FROM"
|
|
|
|
" moz_cache_namespaces AS ns JOIN moz_cache_groups AS groups"
|
|
|
|
" ON ns.ClientID = groups.ActiveClientID"
|
|
|
|
" WHERE ns.NameSpace <= ?1 AND ?1 GLOB ns.NameSpace || '*'"
|
|
|
|
" ORDER BY ns.NameSpace DESC, groups.ActivateTimeStamp DESC;"),
|
2009-02-10 09:40:40 +03:00
|
|
|
StatementSql ( mStatement_FindNamespaceEntry, "SELECT NameSpace, Data, ItemType FROM moz_cache_namespaces"
|
2008-09-30 22:34:23 +04:00
|
|
|
" WHERE ClientID = ?1"
|
|
|
|
" AND NameSpace <= ?2 AND ?2 GLOB NameSpace || '*'"
|
2009-02-10 09:40:40 +03:00
|
|
|
" ORDER BY NameSpace DESC;"),
|
2008-09-30 22:34:23 +04:00
|
|
|
StatementSql ( mStatement_InsertNamespaceEntry, "INSERT INTO moz_cache_namespaces (ClientID, NameSpace, Data, ItemType) VALUES(?, ?, ?, ?);"),
|
2012-03-31 04:52:06 +04:00
|
|
|
StatementSql ( mStatement_EnumerateGroups, "SELECT GroupID, ActiveClientID FROM moz_cache_groups;"),
|
|
|
|
StatementSql ( mStatement_EnumerateGroupsTimeOrder, "SELECT GroupID, ActiveClientID FROM moz_cache_groups ORDER BY ActivateTimeStamp;")
|
2004-10-28 02:19:32 +04:00
|
|
|
};
|
2011-10-11 09:50:08 +04:00
|
|
|
for (PRUint32 i = 0; NS_SUCCEEDED(rv) && i < ArrayLength(prepared); ++i)
|
2004-10-28 02:19:32 +04:00
|
|
|
{
|
2008-08-27 03:09:02 +04:00
|
|
|
LOG(("Creating statement: %s\n", prepared[i].sql));
|
|
|
|
|
|
|
|
rv = mDB->CreateStatement(nsDependentCString(prepared[i].sql),
|
|
|
|
getter_AddRefs(prepared[i].statement));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2004-10-28 02:19:32 +04:00
|
|
|
}
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
rv = InitActiveCaches();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2008-08-25 08:51:19 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsOfflineCacheDevice::InitActiveCaches()
|
|
|
|
{
|
2012-05-18 21:30:49 +04:00
|
|
|
mCaches.Init();
|
|
|
|
mActiveCachesByGroup.Init();
|
2008-08-27 03:09:02 +04:00
|
|
|
|
2012-05-18 21:30:49 +04:00
|
|
|
mActiveCaches.Init(5);
|
2008-08-25 08:51:19 +04:00
|
|
|
|
2012-06-29 02:27:03 +04:00
|
|
|
mLockedEntries.Init(64);
|
|
|
|
|
2009-02-22 22:09:10 +03:00
|
|
|
AutoResetStatement statement(mStatement_EnumerateGroups);
|
2008-08-27 03:09:02 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasRows;
|
2012-05-18 21:30:49 +04:00
|
|
|
nsresult rv = statement->ExecuteStep(&hasRows);
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
while (hasRows)
|
|
|
|
{
|
|
|
|
nsCAutoString group;
|
|
|
|
statement->GetUTF8String(0, group);
|
|
|
|
nsCString clientID;
|
|
|
|
statement->GetUTF8String(1, clientID);
|
|
|
|
|
2011-11-09 00:22:15 +04:00
|
|
|
mActiveCaches.PutEntry(clientID);
|
2008-08-27 03:09:02 +04:00
|
|
|
mActiveCachesByGroup.Put(group, new nsCString(clientID));
|
|
|
|
|
|
|
|
rv = statement->ExecuteStep(&hasRows);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2007-07-26 04:47:33 +04:00
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
/* static */
|
|
|
|
PLDHashOperator
|
|
|
|
nsOfflineCacheDevice::ShutdownApplicationCache(const nsACString &key,
|
|
|
|
nsIWeakReference *weakRef,
|
|
|
|
void *ctx)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIApplicationCache> obj = do_QueryReferent(weakRef);
|
|
|
|
if (obj)
|
|
|
|
{
|
|
|
|
nsApplicationCache *appCache = static_cast<nsApplicationCache*>(obj.get());
|
|
|
|
appCache->MarkInvalid();
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
nsresult
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDevice::Shutdown()
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mDB, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
if (mCaches.IsInitialized())
|
|
|
|
mCaches.EnumerateRead(ShutdownApplicationCache, this);
|
|
|
|
|
2010-08-24 05:06:23 +04:00
|
|
|
{
|
2008-08-27 03:09:02 +04:00
|
|
|
EvictionObserver evictionObserver(mDB, mEvictionFunction);
|
|
|
|
|
|
|
|
// Delete all rows whose clientID is not an active clientID.
|
2008-09-30 22:34:23 +04:00
|
|
|
nsresult rv = mDB->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DELETE FROM moz_cache WHERE rowid IN"
|
|
|
|
" (SELECT moz_cache.rowid FROM"
|
|
|
|
" moz_cache LEFT OUTER JOIN moz_cache_groups ON"
|
|
|
|
" (moz_cache.ClientID = moz_cache_groups.ActiveClientID)"
|
|
|
|
" WHERE moz_cache_groups.GroupID ISNULL)"));
|
2008-08-27 03:09:02 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
NS_WARNING("Failed to clean up unused application caches.");
|
|
|
|
else
|
|
|
|
evictionObserver.Apply();
|
|
|
|
|
2008-09-30 22:34:23 +04:00
|
|
|
// Delete all namespaces whose clientID is not an active clientID.
|
|
|
|
rv = mDB->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
|
|
|
|
"DELETE FROM moz_cache_namespaces WHERE rowid IN"
|
|
|
|
" (SELECT moz_cache_namespaces.rowid FROM"
|
|
|
|
" moz_cache_namespaces LEFT OUTER JOIN moz_cache_groups ON"
|
|
|
|
" (moz_cache_namespaces.ClientID = moz_cache_groups.ActiveClientID)"
|
|
|
|
" WHERE moz_cache_groups.GroupID ISNULL)"));
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
NS_WARNING("Failed to clean up namespaces.");
|
|
|
|
|
2007-07-26 04:47:33 +04:00
|
|
|
mEvictionFunction = 0;
|
|
|
|
|
2010-08-24 05:06:23 +04:00
|
|
|
mStatement_CacheSize = nsnull;
|
|
|
|
mStatement_ApplicationCacheSize = nsnull;
|
|
|
|
mStatement_EntryCount = nsnull;
|
|
|
|
mStatement_UpdateEntry = nsnull;
|
|
|
|
mStatement_UpdateEntrySize = nsnull;
|
|
|
|
mStatement_DeleteEntry = nsnull;
|
|
|
|
mStatement_FindEntry = nsnull;
|
|
|
|
mStatement_BindEntry = nsnull;
|
|
|
|
mStatement_ClearDomain = nsnull;
|
|
|
|
mStatement_MarkEntry = nsnull;
|
|
|
|
mStatement_UnmarkEntry = nsnull;
|
|
|
|
mStatement_GetTypes = nsnull;
|
|
|
|
mStatement_FindNamespaceEntry = nsnull;
|
|
|
|
mStatement_InsertNamespaceEntry = nsnull;
|
|
|
|
mStatement_CleanupUnmarked = nsnull;
|
|
|
|
mStatement_GatherEntries = nsnull;
|
|
|
|
mStatement_ActivateClient = nsnull;
|
|
|
|
mStatement_DeactivateGroup = nsnull;
|
|
|
|
mStatement_FindClient = nsnull;
|
|
|
|
mStatement_FindClientByNamespace = nsnull;
|
|
|
|
mStatement_EnumerateGroups = nsnull;
|
2012-03-31 04:52:06 +04:00
|
|
|
mStatement_EnumerateGroupsTimeOrder = nsnull;
|
2010-08-24 05:06:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close Database on the correct thread
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isOnCurrentThread = true;
|
2010-08-24 05:06:23 +04:00
|
|
|
if (mInitThread)
|
|
|
|
mInitThread->IsOnCurrentThread(&isOnCurrentThread);
|
|
|
|
|
|
|
|
if (!isOnCurrentThread) {
|
|
|
|
nsCOMPtr<nsIRunnable> ev = new nsCloseDBEvent(mDB);
|
|
|
|
|
|
|
|
if (ev) {
|
|
|
|
mInitThread->Dispatch(ev, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mDB->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
mDB = nsnull;
|
|
|
|
mInitThread = nsnull;
|
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDevice::GetDeviceID()
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2007-05-15 00:09:20 +04:00
|
|
|
return OFFLINE_CACHE_DEVICE_ID;
|
2004-10-20 09:00:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCacheEntry *
|
2011-09-29 10:19:26 +04:00
|
|
|
nsOfflineCacheDevice::FindEntry(nsCString *fullKey, bool *collision)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2011-10-30 18:39:53 +04:00
|
|
|
mozilla::Telemetry::AutoTimer<mozilla::Telemetry::CACHE_OFFLINE_SEARCH> timer;
|
2007-05-15 00:09:20 +04:00
|
|
|
LOG(("nsOfflineCacheDevice::FindEntry [key=%s]\n", fullKey->get()));
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
// SELECT * FROM moz_cache WHERE key = ?
|
|
|
|
|
|
|
|
// Decompose the key into "ClientID" and "Key"
|
|
|
|
nsCAutoString keyBuf;
|
|
|
|
const char *cid, *key;
|
|
|
|
if (!DecomposeCacheEntryKey(fullKey, &cid, &key, keyBuf))
|
|
|
|
return nsnull;
|
|
|
|
|
2004-10-28 02:19:32 +04:00
|
|
|
AutoResetStatement statement(mStatement_FindEntry);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2004-10-28 02:19:32 +04:00
|
|
|
nsresult rv;
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = statement->BindUTF8StringByIndex(0, nsDependentCString(cid));
|
|
|
|
rv |= statement->BindUTF8StringByIndex(1, nsDependentCString(key));
|
2004-10-20 09:00:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nsnull);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasRows;
|
2004-10-20 09:00:23 +04:00
|
|
|
rv = statement->ExecuteStep(&hasRows);
|
2004-10-28 02:19:32 +04:00
|
|
|
if (NS_FAILED(rv) || !hasRows)
|
|
|
|
return nsnull; // entry not found
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheRecord rec;
|
|
|
|
statement->GetSharedBlob(0, &rec.metaDataLen,
|
|
|
|
(const PRUint8 **) &rec.metaData);
|
2004-10-20 09:00:23 +04:00
|
|
|
rec.generation = statement->AsInt32(1);
|
2012-06-29 02:26:55 +04:00
|
|
|
rec.dataSize = statement->AsInt32(2);
|
|
|
|
rec.fetchCount = statement->AsInt32(3);
|
|
|
|
rec.lastFetched = statement->AsInt64(4);
|
|
|
|
rec.lastModified = statement->AsInt64(5);
|
|
|
|
rec.expirationTime = statement->AsInt64(6);
|
|
|
|
|
|
|
|
LOG(("entry: [%u %d %d %d %lld %lld %lld]\n",
|
2004-10-20 09:00:23 +04:00
|
|
|
rec.metaDataLen,
|
|
|
|
rec.generation,
|
|
|
|
rec.dataSize,
|
|
|
|
rec.fetchCount,
|
|
|
|
rec.lastFetched,
|
|
|
|
rec.lastModified,
|
|
|
|
rec.expirationTime));
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
nsCacheEntry *entry = CreateCacheEntry(this, fullKey, rec);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
// make sure that the data file exists
|
|
|
|
nsOfflineCacheBinding *binding = (nsOfflineCacheBinding*)entry->Data();
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isFile;
|
2007-05-15 00:09:20 +04:00
|
|
|
rv = binding->mDataFile->IsFile(&isFile);
|
|
|
|
if (NS_FAILED(rv) || !isFile)
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
DeleteEntry(entry, false);
|
2007-05-15 00:09:20 +04:00
|
|
|
delete entry;
|
|
|
|
return nsnull;
|
|
|
|
}
|
2012-06-29 02:27:03 +04:00
|
|
|
|
|
|
|
// lock the entry
|
|
|
|
Lock(*fullKey);
|
2007-05-15 00:09:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
2004-10-20 09:00:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDevice::DeactivateEntry(nsCacheEntry *entry)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2007-05-15 00:09:20 +04:00
|
|
|
LOG(("nsOfflineCacheDevice::DeactivateEntry [key=%s]\n",
|
|
|
|
entry->Key()->get()));
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
// This method is called to inform us that the nsCacheEntry object is going
|
|
|
|
// away. We should persist anything that needs to be persisted, or if the
|
|
|
|
// entry is doomed, we can go ahead and clear its storage.
|
|
|
|
|
|
|
|
if (entry->IsDoomed())
|
|
|
|
{
|
|
|
|
// remove corresponding row and file if they exist
|
|
|
|
|
|
|
|
// the row should have been removed in DoomEntry... we could assert that
|
|
|
|
// that happened. otherwise, all we have to do here is delete the file
|
|
|
|
// on disk.
|
|
|
|
DeleteData(entry);
|
|
|
|
}
|
2012-07-17 04:38:47 +04:00
|
|
|
else if (((nsOfflineCacheBinding *)entry->Data())->IsNewEntry())
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
// UPDATE the database row
|
|
|
|
|
2012-07-17 04:38:47 +04:00
|
|
|
// Only new entries are updated, since offline cache is updated in
|
|
|
|
// transactions. New entries are those who is returned from
|
|
|
|
// BindEntry().
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2012-07-17 04:38:47 +04:00
|
|
|
LOG(("nsOfflineCacheDevice::DeactivateEntry updating new entry\n"));
|
2004-10-20 09:00:23 +04:00
|
|
|
UpdateEntry(entry);
|
2012-07-17 04:38:47 +04:00
|
|
|
} else {
|
|
|
|
LOG(("nsOfflineCacheDevice::DeactivateEntry "
|
|
|
|
"skipping update since entry is not dirty\n"));
|
2004-10-20 09:00:23 +04:00
|
|
|
}
|
|
|
|
|
2012-06-29 02:27:03 +04:00
|
|
|
// Unlock the entry
|
|
|
|
Unlock(*entry->Key());
|
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
delete entry;
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDevice::BindEntry(nsCacheEntry *entry)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2007-05-15 00:09:20 +04:00
|
|
|
LOG(("nsOfflineCacheDevice::BindEntry [key=%s]\n", entry->Key()->get()));
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
NS_ENSURE_STATE(!entry->Data());
|
|
|
|
|
|
|
|
// This method is called to inform us that we have a new entry. The entry
|
|
|
|
// may collide with an existing entry in our DB, but if that happens we can
|
|
|
|
// assume that the entry is not being used.
|
|
|
|
|
|
|
|
// INSERT the database row
|
|
|
|
|
|
|
|
// XXX Assumption: if the row already exists, then FindEntry would have
|
|
|
|
// returned it. if that entry was doomed, then DoomEntry would have removed
|
|
|
|
// it from the table. so, we should always have to insert at this point.
|
|
|
|
|
|
|
|
// Decompose the key into "ClientID" and "Key"
|
|
|
|
nsCAutoString keyBuf;
|
|
|
|
const char *cid, *key;
|
|
|
|
if (!DecomposeCacheEntryKey(entry->Key(), &cid, &key, keyBuf))
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
// create binding, pick best generation number
|
2007-05-15 00:09:20 +04:00
|
|
|
nsRefPtr<nsOfflineCacheBinding> binding =
|
2007-07-25 10:31:27 +04:00
|
|
|
nsOfflineCacheBinding::Create(mCacheDirectory, entry->Key(), -1);
|
2004-10-20 09:00:23 +04:00
|
|
|
if (!binding)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2012-07-17 04:38:47 +04:00
|
|
|
binding->MarkNewEntry();
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheRecord rec;
|
2004-10-20 09:00:23 +04:00
|
|
|
rec.clientID = cid;
|
|
|
|
rec.key = key;
|
|
|
|
rec.metaData = NULL; // don't write any metadata now.
|
|
|
|
rec.metaDataLen = 0;
|
|
|
|
rec.generation = binding->mGeneration;
|
|
|
|
rec.dataSize = 0;
|
|
|
|
rec.fetchCount = entry->FetchCount();
|
|
|
|
rec.lastFetched = PRTimeFromSeconds(entry->LastFetched());
|
|
|
|
rec.lastModified = PRTimeFromSeconds(entry->LastModified());
|
|
|
|
rec.expirationTime = PRTimeFromSeconds(entry->ExpirationTime());
|
|
|
|
|
2004-10-28 02:19:32 +04:00
|
|
|
AutoResetStatement statement(mStatement_BindEntry);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2004-10-28 02:19:32 +04:00
|
|
|
nsresult rv;
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = statement->BindUTF8StringByIndex(0, nsDependentCString(rec.clientID));
|
|
|
|
rv |= statement->BindUTF8StringByIndex(1, nsDependentCString(rec.key));
|
|
|
|
rv |= statement->BindBlobByIndex(2, rec.metaData, rec.metaDataLen);
|
|
|
|
rv |= statement->BindInt32ByIndex(3, rec.generation);
|
2012-06-29 02:26:55 +04:00
|
|
|
rv |= statement->BindInt32ByIndex(4, rec.dataSize);
|
|
|
|
rv |= statement->BindInt32ByIndex(5, rec.fetchCount);
|
|
|
|
rv |= statement->BindInt64ByIndex(6, rec.lastFetched);
|
|
|
|
rv |= statement->BindInt64ByIndex(7, rec.lastModified);
|
|
|
|
rv |= statement->BindInt64ByIndex(8, rec.expirationTime);
|
2004-10-20 09:00:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasRows;
|
2004-10-20 09:00:23 +04:00
|
|
|
rv = statement->ExecuteStep(&hasRows);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ASSERTION(!hasRows, "INSERT should not result in output");
|
|
|
|
|
|
|
|
entry->SetData(binding);
|
2012-06-29 02:27:03 +04:00
|
|
|
|
|
|
|
// lock the entry
|
|
|
|
Lock(*entry->Key());
|
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDevice::DoomEntry(nsCacheEntry *entry)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2007-05-15 00:09:20 +04:00
|
|
|
LOG(("nsOfflineCacheDevice::DoomEntry [key=%s]\n", entry->Key()->get()));
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
// This method is called to inform us that we should mark the entry to be
|
|
|
|
// deleted when it is no longer in use.
|
|
|
|
|
|
|
|
// We can go ahead and delete the corresponding row in our table,
|
|
|
|
// but we must not delete the file on disk until we are deactivated.
|
2012-06-29 02:27:03 +04:00
|
|
|
// In another word, the file should be deleted if the entry had been
|
|
|
|
// deactivated.
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2012-06-29 02:27:03 +04:00
|
|
|
DeleteEntry(entry, !entry->IsActive());
|
2004-10-20 09:00:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDevice::OpenInputStreamForEntry(nsCacheEntry *entry,
|
|
|
|
nsCacheAccessMode mode,
|
|
|
|
PRUint32 offset,
|
|
|
|
nsIInputStream **result)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2007-05-15 00:09:20 +04:00
|
|
|
LOG(("nsOfflineCacheDevice::OpenInputStreamForEntry [key=%s]\n",
|
|
|
|
entry->Key()->get()));
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
*result = nsnull;
|
|
|
|
|
2011-02-12 16:06:02 +03:00
|
|
|
NS_ENSURE_TRUE(!offset || (offset < entry->DataSize()), NS_ERROR_INVALID_ARG);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
// return an input stream to the entry's data file. the stream
|
|
|
|
// may be read on a background thread.
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheBinding *binding = (nsOfflineCacheBinding *) entry->Data();
|
2004-10-20 09:00:23 +04:00
|
|
|
NS_ENSURE_STATE(binding);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> in;
|
|
|
|
NS_NewLocalFileInputStream(getter_AddRefs(in), binding->mDataFile, PR_RDONLY);
|
|
|
|
if (!in)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
// respect |offset| param
|
|
|
|
if (offset != 0)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(in);
|
|
|
|
NS_ENSURE_TRUE(seekable, NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
seekable->Seek(nsISeekableStream::NS_SEEK_SET, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
in.swap(*result);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDevice::OpenOutputStreamForEntry(nsCacheEntry *entry,
|
|
|
|
nsCacheAccessMode mode,
|
|
|
|
PRUint32 offset,
|
|
|
|
nsIOutputStream **result)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2007-05-15 00:09:20 +04:00
|
|
|
LOG(("nsOfflineCacheDevice::OpenOutputStreamForEntry [key=%s]\n",
|
|
|
|
entry->Key()->get()));
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
*result = nsnull;
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(offset <= entry->DataSize(), NS_ERROR_INVALID_ARG);
|
|
|
|
|
|
|
|
// return an output stream to the entry's data file. we can assume
|
|
|
|
// that the output stream will only be used on the main thread.
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheBinding *binding = (nsOfflineCacheBinding *) entry->Data();
|
2004-10-20 09:00:23 +04:00
|
|
|
NS_ENSURE_STATE(binding);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIOutputStream> out;
|
|
|
|
NS_NewLocalFileOutputStream(getter_AddRefs(out), binding->mDataFile,
|
|
|
|
PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE,
|
|
|
|
00600);
|
|
|
|
if (!out)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
// respect |offset| param
|
|
|
|
nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(out);
|
|
|
|
NS_ENSURE_TRUE(seekable, NS_ERROR_UNEXPECTED);
|
|
|
|
if (offset != 0)
|
|
|
|
seekable->Seek(nsISeekableStream::NS_SEEK_SET, offset);
|
|
|
|
|
|
|
|
// truncate the file at the given offset
|
|
|
|
seekable->SetEOF();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIOutputStream> bufferedOut;
|
|
|
|
NS_NewBufferedOutputStream(getter_AddRefs(bufferedOut), out, 16 * 1024);
|
|
|
|
if (!bufferedOut)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
bufferedOut.swap(*result);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDevice::GetFileForEntry(nsCacheEntry *entry, nsIFile **result)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2007-05-15 00:09:20 +04:00
|
|
|
LOG(("nsOfflineCacheDevice::GetFileForEntry [key=%s]\n",
|
|
|
|
entry->Key()->get()));
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheBinding *binding = (nsOfflineCacheBinding *) entry->Data();
|
2004-10-20 09:00:23 +04:00
|
|
|
NS_ENSURE_STATE(binding);
|
|
|
|
|
|
|
|
NS_IF_ADDREF(*result = binding->mDataFile);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDevice::OnDataSizeChange(nsCacheEntry *entry, PRInt32 deltaSize)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2007-05-15 00:09:20 +04:00
|
|
|
LOG(("nsOfflineCacheDevice::OnDataSizeChange [key=%s delta=%d]\n",
|
2004-10-20 09:00:23 +04:00
|
|
|
entry->Key()->get(), deltaSize));
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
const PRInt32 DELTA_THRESHOLD = 1<<14; // 16k
|
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
// called to notify us of an impending change in the total size of the
|
2007-05-15 00:09:20 +04:00
|
|
|
// specified entry.
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
PRUint32 oldSize = entry->DataSize();
|
|
|
|
NS_ASSERTION(deltaSize >= 0 || PRInt32(oldSize) + deltaSize >= 0, "oops");
|
|
|
|
PRUint32 newSize = PRInt32(oldSize) + deltaSize;
|
|
|
|
UpdateEntrySize(entry, newSize);
|
|
|
|
|
|
|
|
mDeltaCounter += deltaSize; // this may go negative
|
|
|
|
|
|
|
|
if (mDeltaCounter >= DELTA_THRESHOLD)
|
|
|
|
{
|
2007-05-15 00:09:20 +04:00
|
|
|
if (CacheSize() > mCacheCapacity) {
|
|
|
|
// the entry will overrun the cache capacity, doom the entry
|
|
|
|
// and abort
|
2008-09-15 17:34:46 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsresult rv =
|
|
|
|
#endif
|
|
|
|
nsCacheService::DoomEntry(entry);
|
2007-05-15 00:09:20 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "DoomEntry() failed.");
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
mDeltaCounter = 0; // reset counter
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDevice::Visit(nsICacheVisitor *visitor)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(Initialized(), NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
// called to enumerate the offline cache.
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsICacheDeviceInfo> deviceInfo =
|
2007-05-15 00:09:20 +04:00
|
|
|
new nsOfflineCacheDeviceInfo(this);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool keepGoing;
|
2007-05-15 00:09:20 +04:00
|
|
|
nsresult rv = visitor->VisitDevice(OFFLINE_CACHE_DEVICE_ID, deviceInfo,
|
2004-10-20 09:00:23 +04:00
|
|
|
&keepGoing);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (!keepGoing)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// SELECT * from moz_cache;
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheRecord rec;
|
|
|
|
nsRefPtr<nsOfflineCacheEntryInfo> info = new nsOfflineCacheEntryInfo;
|
2004-10-20 09:00:23 +04:00
|
|
|
if (!info)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
info->mRec = &rec;
|
|
|
|
|
2004-10-28 02:19:32 +04:00
|
|
|
// XXX may want to list columns explicitly
|
2004-10-20 09:00:23 +04:00
|
|
|
nsCOMPtr<mozIStorageStatement> statement;
|
|
|
|
rv = mDB->CreateStatement(
|
|
|
|
NS_LITERAL_CSTRING("SELECT * FROM moz_cache;"),
|
|
|
|
getter_AddRefs(statement));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasRows;
|
2004-10-20 09:00:23 +04:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
rv = statement->ExecuteStep(&hasRows);
|
|
|
|
if (NS_FAILED(rv) || !hasRows)
|
|
|
|
break;
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
statement->GetSharedUTF8String(0, NULL, &rec.clientID);
|
|
|
|
statement->GetSharedUTF8String(1, NULL, &rec.key);
|
|
|
|
statement->GetSharedBlob(2, &rec.metaDataLen,
|
|
|
|
(const PRUint8 **) &rec.metaData);
|
2004-10-20 09:00:23 +04:00
|
|
|
rec.generation = statement->AsInt32(3);
|
2012-06-29 02:26:55 +04:00
|
|
|
rec.dataSize = statement->AsInt32(4);
|
|
|
|
rec.fetchCount = statement->AsInt32(5);
|
|
|
|
rec.lastFetched = statement->AsInt64(6);
|
|
|
|
rec.lastModified = statement->AsInt64(7);
|
|
|
|
rec.expirationTime = statement->AsInt64(8);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool keepGoing;
|
2007-05-15 00:09:20 +04:00
|
|
|
rv = visitor->VisitEntry(OFFLINE_CACHE_DEVICE_ID, info, &keepGoing);
|
2004-10-20 09:00:23 +04:00
|
|
|
if (NS_FAILED(rv) || !keepGoing)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->mRec = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-01 02:18:08 +04:00
|
|
|
nsresult
|
|
|
|
nsOfflineCacheDevice::EvictEntries(const char *clientID)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2012-04-01 02:18:08 +04:00
|
|
|
LOG(("nsOfflineCacheDevice::EvictEntries [cid=%s]\n",
|
|
|
|
clientID ? clientID : ""));
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2012-03-31 04:52:07 +04:00
|
|
|
// called to evict all entries matching the given clientID.
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2012-04-01 02:18:08 +04:00
|
|
|
// need trigger to fire user defined function after a row is deleted
|
|
|
|
// so we can delete the corresponding data file.
|
|
|
|
EvictionObserver evictionObserver(mDB, mEvictionFunction);
|
|
|
|
|
2012-03-31 04:52:07 +04:00
|
|
|
nsCOMPtr<mozIStorageStatement> statement;
|
2012-04-01 02:18:08 +04:00
|
|
|
nsresult rv;
|
|
|
|
if (clientID)
|
|
|
|
{
|
2012-06-29 02:26:55 +04:00
|
|
|
rv = mDB->CreateStatement(NS_LITERAL_CSTRING("DELETE FROM moz_cache WHERE ClientID=?;"),
|
2012-04-01 02:18:08 +04:00
|
|
|
getter_AddRefs(statement));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2008-08-27 03:09:02 +04:00
|
|
|
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = statement->BindUTF8StringByIndex(0, nsDependentCString(clientID));
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-03-31 04:52:06 +04:00
|
|
|
|
2012-04-01 02:18:08 +04:00
|
|
|
rv = statement->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-03-31 04:52:06 +04:00
|
|
|
|
2012-04-01 02:18:08 +04:00
|
|
|
rv = mDB->CreateStatement(NS_LITERAL_CSTRING("DELETE FROM moz_cache_groups WHERE ActiveClientID=?;"),
|
|
|
|
getter_AddRefs(statement));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-03-31 04:52:06 +04:00
|
|
|
|
2012-04-01 02:18:08 +04:00
|
|
|
rv = statement->BindUTF8StringByIndex(0, nsDependentCString(clientID));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2012-04-01 02:18:08 +04:00
|
|
|
rv = statement->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-29 02:26:55 +04:00
|
|
|
rv = mDB->CreateStatement(NS_LITERAL_CSTRING("DELETE FROM moz_cache;"),
|
2012-04-01 02:18:08 +04:00
|
|
|
getter_AddRefs(statement));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-03-31 04:52:06 +04:00
|
|
|
|
2012-04-01 02:18:08 +04:00
|
|
|
rv = statement->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-03-31 04:52:06 +04:00
|
|
|
|
2012-04-01 02:18:08 +04:00
|
|
|
rv = mDB->CreateStatement(NS_LITERAL_CSTRING("DELETE FROM moz_cache_groups;"),
|
|
|
|
getter_AddRefs(statement));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-03-31 04:52:07 +04:00
|
|
|
|
2012-04-01 02:18:08 +04:00
|
|
|
rv = statement->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-03-31 04:52:06 +04:00
|
|
|
}
|
2008-08-27 03:09:02 +04:00
|
|
|
|
|
|
|
evictionObserver.Apply();
|
|
|
|
|
2012-04-01 02:18:08 +04:00
|
|
|
statement = nsnull;
|
|
|
|
// Also evict any namespaces associated with this clientID.
|
|
|
|
if (clientID)
|
|
|
|
{
|
|
|
|
rv = mDB->CreateStatement(NS_LITERAL_CSTRING("DELETE FROM moz_cache_namespaces WHERE ClientID=?"),
|
|
|
|
getter_AddRefs(statement));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2008-09-30 22:34:23 +04:00
|
|
|
|
2012-04-01 02:18:08 +04:00
|
|
|
rv = statement->BindUTF8StringByIndex(0, nsDependentCString(clientID));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rv = mDB->CreateStatement(NS_LITERAL_CSTRING("DELETE FROM moz_cache_namespaces;"),
|
|
|
|
getter_AddRefs(statement));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2008-09-30 22:34:23 +04:00
|
|
|
|
2012-04-01 02:18:08 +04:00
|
|
|
rv = statement->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2008-09-30 22:34:23 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsOfflineCacheDevice::MarkEntry(const nsCString &clientID,
|
|
|
|
const nsACString &key,
|
|
|
|
PRUint32 typeBits)
|
|
|
|
{
|
|
|
|
LOG(("nsOfflineCacheDevice::MarkEntry [cid=%s, key=%s, typeBits=%d]\n",
|
|
|
|
clientID.get(), PromiseFlatCString(key).get(), typeBits));
|
|
|
|
|
|
|
|
AutoResetStatement statement(mStatement_MarkEntry);
|
2011-03-31 21:19:31 +04:00
|
|
|
nsresult rv = statement->BindInt32ByIndex(0, typeBits);
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = statement->BindUTF8StringByIndex(1, clientID);
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = statement->BindUTF8StringByIndex(2, key);
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = statement->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsOfflineCacheDevice::UnmarkEntry(const nsCString &clientID,
|
|
|
|
const nsACString &key,
|
|
|
|
PRUint32 typeBits)
|
|
|
|
{
|
|
|
|
LOG(("nsOfflineCacheDevice::UnmarkEntry [cid=%s, key=%s, typeBits=%d]\n",
|
|
|
|
clientID.get(), PromiseFlatCString(key).get(), typeBits));
|
|
|
|
|
|
|
|
AutoResetStatement statement(mStatement_UnmarkEntry);
|
2011-03-31 21:19:31 +04:00
|
|
|
nsresult rv = statement->BindInt32ByIndex(0, typeBits);
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = statement->BindUTF8StringByIndex(1, clientID);
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = statement->BindUTF8StringByIndex(2, key);
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = statement->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Remove the entry if it is now empty.
|
|
|
|
|
|
|
|
EvictionObserver evictionObserver(mDB, mEvictionFunction);
|
|
|
|
|
|
|
|
AutoResetStatement cleanupStatement(mStatement_CleanupUnmarked);
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = cleanupStatement->BindUTF8StringByIndex(0, clientID);
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = cleanupStatement->BindUTF8StringByIndex(1, key);
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = cleanupStatement->Execute();
|
2004-10-20 09:00:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-07-26 04:47:33 +04:00
|
|
|
evictionObserver.Apply();
|
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-09-30 22:34:23 +04:00
|
|
|
nsresult
|
|
|
|
nsOfflineCacheDevice::GetMatchingNamespace(const nsCString &clientID,
|
|
|
|
const nsACString &key,
|
|
|
|
nsIApplicationCacheNamespace **out)
|
|
|
|
{
|
|
|
|
LOG(("nsOfflineCacheDevice::GetMatchingNamespace [cid=%s, key=%s]\n",
|
|
|
|
clientID.get(), PromiseFlatCString(key).get()));
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
AutoResetStatement statement(mStatement_FindNamespaceEntry);
|
|
|
|
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = statement->BindUTF8StringByIndex(0, clientID);
|
2008-09-30 22:34:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = statement->BindUTF8StringByIndex(1, key);
|
2008-09-30 22:34:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasRows;
|
2008-09-30 22:34:23 +04:00
|
|
|
rv = statement->ExecuteStep(&hasRows);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
*out = nsnull;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool found = false;
|
2009-02-22 22:09:10 +03:00
|
|
|
nsCString nsSpec;
|
2012-04-08 01:25:00 +04:00
|
|
|
PRInt32 nsType = 0;
|
2009-02-22 22:09:10 +03:00
|
|
|
nsCString nsData;
|
|
|
|
|
|
|
|
while (hasRows)
|
2008-09-30 22:34:23 +04:00
|
|
|
{
|
2009-02-22 22:09:10 +03:00
|
|
|
PRInt32 itemType;
|
|
|
|
rv = statement->GetInt32(2, &itemType);
|
2008-09-30 22:34:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2009-02-22 22:09:10 +03:00
|
|
|
if (!found || itemType > nsType)
|
|
|
|
{
|
|
|
|
nsType = itemType;
|
2008-09-30 22:34:23 +04:00
|
|
|
|
2009-02-22 22:09:10 +03:00
|
|
|
rv = statement->GetUTF8String(0, nsSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = statement->GetUTF8String(1, nsData);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
found = true;
|
2009-02-22 22:09:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = statement->ExecuteStep(&hasRows);
|
2008-09-30 22:34:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-02-22 22:09:10 +03:00
|
|
|
}
|
2008-09-30 22:34:23 +04:00
|
|
|
|
2009-02-22 22:09:10 +03:00
|
|
|
if (found) {
|
2008-09-30 22:34:23 +04:00
|
|
|
nsCOMPtr<nsIApplicationCacheNamespace> ns =
|
|
|
|
new nsApplicationCacheNamespace();
|
|
|
|
if (!ns)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2009-02-22 22:09:10 +03:00
|
|
|
rv = ns->Init(nsType, nsSpec, nsData);
|
2008-09-30 22:34:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
ns.swap(*out);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsOfflineCacheDevice::CacheOpportunistically(const nsCString &clientID,
|
|
|
|
const nsACString &key)
|
|
|
|
{
|
|
|
|
// XXX: We should also be propagating this cache entry to other matching
|
|
|
|
// caches. See bug 444807.
|
|
|
|
|
|
|
|
return MarkEntry(clientID, key, nsIApplicationCache::ITEM_OPPORTUNISTIC);
|
|
|
|
}
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
nsresult
|
|
|
|
nsOfflineCacheDevice::GetTypes(const nsCString &clientID,
|
|
|
|
const nsACString &key,
|
|
|
|
PRUint32 *typeBits)
|
|
|
|
{
|
|
|
|
LOG(("nsOfflineCacheDevice::GetTypes [cid=%s, key=%s]\n",
|
|
|
|
clientID.get(), PromiseFlatCString(key).get()));
|
|
|
|
|
|
|
|
AutoResetStatement statement(mStatement_GetTypes);
|
2011-03-31 21:19:31 +04:00
|
|
|
nsresult rv = statement->BindUTF8StringByIndex(0, clientID);
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = statement->BindUTF8StringByIndex(1, key);
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasRows;
|
2008-08-27 03:09:02 +04:00
|
|
|
rv = statement->ExecuteStep(&hasRows);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!hasRows)
|
|
|
|
return NS_ERROR_CACHE_KEY_NOT_FOUND;
|
|
|
|
|
|
|
|
*typeBits = statement->AsInt32(0);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsOfflineCacheDevice::GatherEntries(const nsCString &clientID,
|
|
|
|
PRUint32 typeBits,
|
|
|
|
PRUint32 *count,
|
|
|
|
char ***keys)
|
|
|
|
{
|
|
|
|
LOG(("nsOfflineCacheDevice::GatherEntries [cid=%s, typeBits=%X]\n",
|
|
|
|
clientID.get(), typeBits));
|
|
|
|
|
|
|
|
AutoResetStatement statement(mStatement_GatherEntries);
|
2011-03-31 21:19:31 +04:00
|
|
|
nsresult rv = statement->BindUTF8StringByIndex(0, clientID);
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = statement->BindInt32ByIndex(1, typeBits);
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return RunSimpleQuery(mStatement_GatherEntries, 0, count, keys);
|
|
|
|
}
|
|
|
|
|
2008-09-30 22:34:23 +04:00
|
|
|
nsresult
|
|
|
|
nsOfflineCacheDevice::AddNamespace(const nsCString &clientID,
|
|
|
|
nsIApplicationCacheNamespace *ns)
|
|
|
|
{
|
|
|
|
nsCString namespaceSpec;
|
|
|
|
nsresult rv = ns->GetNamespaceSpec(namespaceSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCString data;
|
|
|
|
rv = ns->GetData(data);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRUint32 itemType;
|
|
|
|
rv = ns->GetItemType(&itemType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-11-06 03:01:07 +03:00
|
|
|
LOG(("nsOfflineCacheDevice::AddNamespace [cid=%s, ns=%s, data=%s, type=%d]",
|
2011-06-17 00:25:20 +04:00
|
|
|
clientID.get(), namespaceSpec.get(), data.get(), itemType));
|
2008-09-30 22:34:23 +04:00
|
|
|
|
|
|
|
AutoResetStatement statement(mStatement_InsertNamespaceEntry);
|
|
|
|
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = statement->BindUTF8StringByIndex(0, clientID);
|
2008-09-30 22:34:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = statement->BindUTF8StringByIndex(1, namespaceSpec);
|
2008-09-30 22:34:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = statement->BindUTF8StringByIndex(2, data);
|
2008-09-30 22:34:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = statement->BindInt32ByIndex(3, itemType);
|
2008-09-30 22:34:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = statement->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-02-22 22:09:10 +03:00
|
|
|
nsresult
|
|
|
|
nsOfflineCacheDevice::GetUsage(const nsACString &clientID,
|
|
|
|
PRUint32 *usage)
|
|
|
|
{
|
|
|
|
LOG(("nsOfflineCacheDevice::GetUsage [cid=%s]\n",
|
|
|
|
PromiseFlatCString(clientID).get()));
|
|
|
|
|
|
|
|
*usage = 0;
|
|
|
|
|
|
|
|
AutoResetStatement statement(mStatement_ApplicationCacheSize);
|
|
|
|
|
2011-03-31 21:19:31 +04:00
|
|
|
nsresult rv = statement->BindUTF8StringByIndex(0, clientID);
|
2009-02-22 22:09:10 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasRows;
|
2009-02-22 22:09:10 +03:00
|
|
|
rv = statement->ExecuteStep(&hasRows);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!hasRows)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
*usage = static_cast<PRUint32>(statement->AsInt32(0));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-30 21:57:09 +04:00
|
|
|
nsresult
|
2009-02-22 22:09:10 +03:00
|
|
|
nsOfflineCacheDevice::GetGroups(PRUint32 *count,
|
|
|
|
char ***keys)
|
|
|
|
{
|
|
|
|
LOG(("nsOfflineCacheDevice::GetGroups"));
|
|
|
|
|
|
|
|
return RunSimpleQuery(mStatement_EnumerateGroups, 0, count, keys);
|
|
|
|
}
|
|
|
|
|
2012-04-30 21:57:09 +04:00
|
|
|
nsresult
|
2012-03-31 04:52:06 +04:00
|
|
|
nsOfflineCacheDevice::GetGroupsTimeOrdered(PRUint32 *count,
|
|
|
|
char ***keys)
|
|
|
|
{
|
|
|
|
LOG(("nsOfflineCacheDevice::GetGroupsTimeOrder"));
|
|
|
|
|
|
|
|
return RunSimpleQuery(mStatement_EnumerateGroupsTimeOrder, 0, count, keys);
|
|
|
|
}
|
|
|
|
|
2012-06-29 02:27:03 +04:00
|
|
|
bool
|
|
|
|
nsOfflineCacheDevice::IsLocked(const nsACString &key)
|
|
|
|
{
|
|
|
|
return mLockedEntries.GetEntry(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsOfflineCacheDevice::Lock(const nsACString &key)
|
|
|
|
{
|
|
|
|
mLockedEntries.PutEntry(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsOfflineCacheDevice::Unlock(const nsACString &key)
|
|
|
|
{
|
|
|
|
mLockedEntries.RemoveEntry(key);
|
|
|
|
}
|
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
nsresult
|
|
|
|
nsOfflineCacheDevice::RunSimpleQuery(mozIStorageStatement * statement,
|
|
|
|
PRUint32 resultIndex,
|
|
|
|
PRUint32 * count,
|
|
|
|
char *** values)
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasRows;
|
2007-07-25 09:35:39 +04:00
|
|
|
nsresult rv = statement->ExecuteStep(&hasRows);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsTArray<nsCString> valArray;
|
|
|
|
while (hasRows)
|
|
|
|
{
|
|
|
|
PRUint32 length;
|
|
|
|
valArray.AppendElement(
|
|
|
|
nsDependentCString(statement->AsSharedUTF8String(resultIndex, &length)));
|
|
|
|
|
|
|
|
rv = statement->ExecuteStep(&hasRows);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
*count = valArray.Length();
|
|
|
|
char **ret = static_cast<char **>(NS_Alloc(*count * sizeof(char*)));
|
|
|
|
if (!ret) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < *count; i++) {
|
|
|
|
ret[i] = NS_strdup(valArray[i].get());
|
|
|
|
if (!ret[i]) {
|
|
|
|
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(i, ret);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*values = ret;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-30 21:57:09 +04:00
|
|
|
nsresult
|
2008-08-27 03:09:02 +04:00
|
|
|
nsOfflineCacheDevice::CreateApplicationCache(const nsACString &group,
|
|
|
|
nsIApplicationCache **out)
|
2008-08-25 10:11:31 +04:00
|
|
|
{
|
2008-08-27 03:09:02 +04:00
|
|
|
*out = nsnull;
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
nsCString clientID;
|
|
|
|
// Some characters are special in the clientID. Escape the groupID
|
|
|
|
// before putting it in to the client key.
|
|
|
|
if (!NS_Escape(nsCString(group), clientID, url_Path)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
PRTime now = PR_Now();
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
// Include the timestamp to guarantee uniqueness across runs, and
|
|
|
|
// the gNextTemporaryClientID for uniqueness within a second.
|
2012-04-24 22:43:00 +04:00
|
|
|
clientID.Append(nsPrintfCString("|%016lld|%d",
|
2008-08-27 03:09:02 +04:00
|
|
|
now / PR_USEC_PER_SEC,
|
|
|
|
gNextTemporaryClientID++));
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
nsCOMPtr<nsIApplicationCache> cache = new nsApplicationCache(this,
|
|
|
|
group,
|
|
|
|
clientID);
|
|
|
|
if (!cache)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2008-08-25 10:11:31 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
nsCOMPtr<nsIWeakReference> weak = do_GetWeakReference(cache);
|
|
|
|
if (!weak)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
mCaches.Put(clientID, weak);
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
cache.swap(*out);
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
return NS_OK;
|
2008-08-25 10:11:31 +04:00
|
|
|
}
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2012-04-30 21:57:09 +04:00
|
|
|
nsresult
|
2008-08-27 03:09:02 +04:00
|
|
|
nsOfflineCacheDevice::GetApplicationCache(const nsACString &clientID,
|
|
|
|
nsIApplicationCache **out)
|
2008-08-25 10:11:31 +04:00
|
|
|
{
|
2008-08-27 03:09:02 +04:00
|
|
|
*out = nsnull;
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
nsCOMPtr<nsIApplicationCache> cache;
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
nsWeakPtr weak;
|
|
|
|
if (mCaches.Get(clientID, getter_AddRefs(weak)))
|
|
|
|
cache = do_QueryReferent(weak);
|
2008-08-20 09:53:20 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
if (!cache)
|
|
|
|
{
|
|
|
|
nsCString group;
|
|
|
|
nsresult rv = GetGroupForCache(clientID, group);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
if (group.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
cache = new nsApplicationCache(this, group, clientID);
|
|
|
|
weak = do_GetWeakReference(cache);
|
|
|
|
if (!weak)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2008-08-25 10:11:31 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
mCaches.Put(clientID, weak);
|
|
|
|
}
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
cache.swap(*out);
|
2008-08-20 09:53:20 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
return NS_OK;
|
2008-08-20 09:53:20 +04:00
|
|
|
}
|
|
|
|
|
2012-04-30 21:57:09 +04:00
|
|
|
nsresult
|
2008-08-27 03:09:02 +04:00
|
|
|
nsOfflineCacheDevice::GetActiveCache(const nsACString &group,
|
|
|
|
nsIApplicationCache **out)
|
2008-08-20 09:53:20 +04:00
|
|
|
{
|
2008-08-27 03:09:02 +04:00
|
|
|
*out = nsnull;
|
2008-08-20 09:53:20 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
nsCString *clientID;
|
|
|
|
if (mActiveCachesByGroup.Get(group, &clientID))
|
|
|
|
return GetApplicationCache(*clientID, out);
|
2008-08-25 10:11:31 +04:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
return NS_OK;
|
2008-08-25 10:11:31 +04:00
|
|
|
}
|
|
|
|
|
2012-04-30 21:57:09 +04:00
|
|
|
nsresult
|
2008-11-04 21:59:12 +03:00
|
|
|
nsOfflineCacheDevice::DeactivateGroup(const nsACString &group)
|
|
|
|
{
|
|
|
|
nsCString *active = nsnull;
|
|
|
|
|
|
|
|
AutoResetStatement statement(mStatement_DeactivateGroup);
|
2011-03-31 21:19:31 +04:00
|
|
|
nsresult rv = statement->BindUTF8StringByIndex(0, group);
|
2008-11-04 21:59:12 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = statement->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (mActiveCachesByGroup.Get(group, &active))
|
|
|
|
{
|
2011-11-09 00:22:15 +04:00
|
|
|
mActiveCaches.RemoveEntry(*active);
|
2008-11-04 21:59:12 +03:00
|
|
|
mActiveCachesByGroup.Remove(group);
|
|
|
|
active = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2008-09-30 22:34:23 +04:00
|
|
|
nsOfflineCacheDevice::CanUseCache(nsIURI *keyURI, const nsCString &clientID)
|
|
|
|
{
|
|
|
|
if (mActiveCaches.Contains(clientID)) {
|
|
|
|
nsCAutoString groupID;
|
|
|
|
nsresult rv = GetGroupForCache(clientID, groupID);
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2008-09-30 22:34:23 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> groupURI;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(groupURI), groupID);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// When we are choosing an initial cache to load the top
|
|
|
|
// level document from, the URL of that document must have
|
|
|
|
// the same origin as the manifest, according to the spec.
|
|
|
|
// The following check is here because explicit, fallback
|
|
|
|
// and dynamic entries might have origin different from the
|
|
|
|
// manifest origin.
|
|
|
|
if (NS_SecurityCompareURIs(keyURI, groupURI,
|
|
|
|
GetStrictFileOriginPolicy()))
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-09-30 22:34:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2008-09-30 22:34:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-30 21:57:09 +04:00
|
|
|
nsresult
|
2008-08-27 03:09:02 +04:00
|
|
|
nsOfflineCacheDevice::ChooseApplicationCache(const nsACString &key,
|
|
|
|
nsIApplicationCache **out)
|
2008-08-25 10:11:31 +04:00
|
|
|
{
|
2008-08-27 03:09:02 +04:00
|
|
|
*out = nsnull;
|
2008-08-25 10:11:31 +04:00
|
|
|
|
2008-09-30 22:34:23 +04:00
|
|
|
nsCOMPtr<nsIURI> keyURI;
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(keyURI), key);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// First try to find a matching cache entry.
|
2008-08-27 03:09:02 +04:00
|
|
|
AutoResetStatement statement(mStatement_FindClient);
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = statement->BindUTF8StringByIndex(0, key);
|
2008-02-07 12:04:30 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasRows;
|
2008-02-07 12:04:30 +03:00
|
|
|
rv = statement->ExecuteStep(&hasRows);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
while (hasRows) {
|
2008-08-28 05:15:32 +04:00
|
|
|
PRInt32 itemType;
|
|
|
|
rv = statement->GetInt32(1, &itemType);
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-08-28 05:15:32 +04:00
|
|
|
if (!(itemType & nsIApplicationCache::ITEM_FOREIGN)) {
|
|
|
|
nsCAutoString clientID;
|
|
|
|
rv = statement->GetUTF8String(0, clientID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-09-30 22:34:23 +04:00
|
|
|
if (CanUseCache(keyURI, clientID)) {
|
|
|
|
return GetApplicationCache(clientID, out);
|
2008-08-28 05:15:32 +04:00
|
|
|
}
|
|
|
|
}
|
2008-02-07 12:04:30 +03:00
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
rv = statement->ExecuteStep(&hasRows);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2008-02-07 12:04:30 +03:00
|
|
|
|
2008-09-30 22:34:23 +04:00
|
|
|
// OK, we didn't find an exact match. Search for a client with a
|
|
|
|
// matching namespace.
|
|
|
|
|
|
|
|
AutoResetStatement nsstatement(mStatement_FindClientByNamespace);
|
|
|
|
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = nsstatement->BindUTF8StringByIndex(0, key);
|
2008-09-30 22:34:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = nsstatement->ExecuteStep(&hasRows);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
while (hasRows)
|
|
|
|
{
|
|
|
|
PRInt32 itemType;
|
|
|
|
rv = nsstatement->GetInt32(1, &itemType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Don't associate with a cache based solely on a whitelist entry
|
|
|
|
if (!(itemType & nsIApplicationCacheNamespace::NAMESPACE_BYPASS)) {
|
|
|
|
nsCAutoString clientID;
|
|
|
|
rv = nsstatement->GetUTF8String(0, clientID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (CanUseCache(keyURI, clientID)) {
|
|
|
|
return GetApplicationCache(clientID, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = nsstatement->ExecuteStep(&hasRows);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2008-02-07 12:04:30 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-30 21:57:09 +04:00
|
|
|
nsresult
|
2008-09-30 22:34:23 +04:00
|
|
|
nsOfflineCacheDevice::CacheOpportunistically(nsIApplicationCache* cache,
|
|
|
|
const nsACString &key)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(cache);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCAutoString clientID;
|
|
|
|
rv = cache->GetClientID(clientID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return CacheOpportunistically(clientID, key);
|
|
|
|
}
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
nsresult
|
2008-08-27 03:09:02 +04:00
|
|
|
nsOfflineCacheDevice::ActivateCache(const nsCSubstring &group,
|
|
|
|
const nsCSubstring &clientID)
|
2007-05-15 00:09:20 +04:00
|
|
|
{
|
2008-08-27 03:09:02 +04:00
|
|
|
AutoResetStatement statement(mStatement_ActivateClient);
|
2011-03-31 21:19:31 +04:00
|
|
|
nsresult rv = statement->BindUTF8StringByIndex(0, group);
|
2008-08-27 03:09:02 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = statement->BindUTF8StringByIndex(1, clientID);
|
2007-05-15 00:09:20 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-03-31 21:19:31 +04:00
|
|
|
rv = statement->BindInt32ByIndex(2, SecondsFromPRTime(PR_Now()));
|
2008-09-30 22:34:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2007-07-26 04:47:33 +04:00
|
|
|
rv = statement->Execute();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
nsCString *active;
|
|
|
|
if (mActiveCachesByGroup.Get(group, &active))
|
|
|
|
{
|
2011-11-09 00:22:15 +04:00
|
|
|
mActiveCaches.RemoveEntry(*active);
|
2008-08-27 03:09:02 +04:00
|
|
|
mActiveCachesByGroup.Remove(group);
|
|
|
|
active = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!clientID.IsEmpty())
|
|
|
|
{
|
2011-11-09 00:22:15 +04:00
|
|
|
mActiveCaches.PutEntry(clientID);
|
2008-08-27 03:09:02 +04:00
|
|
|
mActiveCachesByGroup.Put(group, new nsCString(clientID));
|
|
|
|
}
|
|
|
|
|
2007-07-26 04:47:33 +04:00
|
|
|
return NS_OK;
|
2007-05-15 00:09:20 +04:00
|
|
|
}
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2008-08-27 03:09:02 +04:00
|
|
|
nsOfflineCacheDevice::IsActiveCache(const nsCSubstring &group,
|
|
|
|
const nsCSubstring &clientID)
|
2007-07-25 10:31:27 +04:00
|
|
|
{
|
2008-08-27 03:09:02 +04:00
|
|
|
nsCString *active = nsnull;
|
|
|
|
return mActiveCachesByGroup.Get(group, &active) && *active == clientID;
|
2007-07-25 10:31:27 +04:00
|
|
|
}
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
nsresult
|
|
|
|
nsOfflineCacheDevice::GetGroupForCache(const nsACString &clientID,
|
|
|
|
nsCString &out)
|
|
|
|
{
|
|
|
|
out.Assign(clientID);
|
|
|
|
out.Truncate(out.FindChar('|'));
|
|
|
|
NS_UnescapeURL(out);
|
2007-07-26 04:47:33 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2007-07-25 10:31:27 +04:00
|
|
|
}
|
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
/**
|
|
|
|
* Preference accessors
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2012-06-06 06:08:30 +04:00
|
|
|
nsOfflineCacheDevice::SetCacheParentDirectory(nsIFile *parentDir)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
if (Initialized())
|
|
|
|
{
|
|
|
|
NS_ERROR("cannot switch cache directory once initialized");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parentDir)
|
|
|
|
{
|
|
|
|
mCacheDirectory = nsnull;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure parent directory exists
|
|
|
|
nsresult rv = EnsureDir(parentDir);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
NS_WARNING("unable to create parent directory");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-04 18:12:24 +04:00
|
|
|
mBaseDirectory = parentDir;
|
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
// cache dir may not exist, but that's ok
|
|
|
|
nsCOMPtr<nsIFile> dir;
|
|
|
|
rv = parentDir->Clone(getter_AddRefs(dir));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
2007-05-15 00:09:20 +04:00
|
|
|
rv = dir->AppendNative(NS_LITERAL_CSTRING("OfflineCache"));
|
2004-10-20 09:00:23 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
|
|
|
|
|
|
|
mCacheDirectory = do_QueryInterface(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDevice::SetCapacity(PRUint32 capacity)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
mCacheCapacity = capacity * 1024;
|
|
|
|
}
|
2012-07-11 22:20:17 +04:00
|
|
|
|
|
|
|
bool
|
|
|
|
nsOfflineCacheDevice::AutoShutdown(nsIApplicationCache * aAppCache)
|
|
|
|
{
|
|
|
|
if (!mAutoShutdown)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
mAutoShutdown = false;
|
|
|
|
|
|
|
|
Shutdown();
|
|
|
|
|
|
|
|
nsRefPtr<nsCacheService> cacheService = nsCacheService::GlobalInstance();
|
|
|
|
cacheService->RemoveCustomOfflineDevice(this);
|
|
|
|
|
|
|
|
nsCAutoString clientID;
|
|
|
|
aAppCache->GetClientID(clientID);
|
|
|
|
mCaches.Remove(clientID);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|