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
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.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"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsIURI.h"
|
2004-10-20 09:00:23 +04:00
|
|
|
#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"
|
2013-09-20 13:11:25 +04:00
|
|
|
#include "nsILoadContextInfo.h"
|
2010-08-24 05:06:23 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2013-01-30 01:05:43 +04:00
|
|
|
#include "nsISerializable.h"
|
|
|
|
#include "nsSerializationHelper.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"
|
|
|
|
|
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;
|
2015-10-11 19:13:09 +03:00
|
|
|
using mozilla::OriginAttributes;
|
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)
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static uint32_t 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;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t colon = buf.FindChar(':');
|
2004-10-20 09:00:23 +04:00
|
|
|
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:
|
2014-08-05 17:20:24 +04:00
|
|
|
explicit AutoResetStatement(mozIStorageStatement *s)
|
2004-10-28 02:19:32 +04:00
|
|
|
: mStatement(s) {}
|
|
|
|
~AutoResetStatement() { mStatement->Reset(); }
|
2014-12-25 23:18:38 +03:00
|
|
|
mozIStorageStatement *operator->() MOZ_NO_ADDREF_RELEASE_ON_RETURN { return mStatement; }
|
2004-10-28 02:19:32 +04:00
|
|
|
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
|
|
|
|
2012-09-28 23:55:23 +04:00
|
|
|
#define DCACHE_HASH_MAX INT64_MAX
|
2004-10-20 09:00:23 +04:00
|
|
|
#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.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
static uint64_t
|
2004-10-20 09:00:23 +04:00
|
|
|
DCacheHash(const char * key)
|
|
|
|
{
|
2009-03-27 20:58:06 +03:00
|
|
|
// initval 0x7416f295 was chosen randomly
|
2012-08-22 19:56:38 +04:00
|
|
|
return (uint64_t(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
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(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
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint64_t hash = DCacheHash(key);
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t dir1 = (uint32_t) (hash & 0x0F);
|
|
|
|
uint32_t dir2 = (uint32_t)((hash & 0xF0) >> 4);
|
2007-05-15 00:09:20 +04:00
|
|
|
|
|
|
|
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"));
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*_retval = nullptr;
|
2007-05-15 00:09:20 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t numEntries;
|
2007-05-15 00:09:20 +04:00
|
|
|
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
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t valueLen;
|
2012-06-29 02:27:03 +04:00
|
|
|
const char *clientID = values->AsSharedUTF8String(0, &valueLen);
|
|
|
|
const char *key = values->AsSharedUTF8String(1, &valueLen);
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString fullKey(clientID);
|
2014-05-22 07:48:51 +04:00
|
|
|
fullKey.Append(':');
|
2012-06-29 02:27:03 +04:00
|
|
|
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-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < mItems.Count(); i++) {
|
2015-06-04 01:25:57 +03:00
|
|
|
if (MOZ_LOG_TEST(gCacheLog, LogLevel::Debug)) {
|
2015-05-07 22:52:45 +03:00
|
|
|
nsAutoCString path;
|
|
|
|
mItems[i]->GetNativePath(path);
|
|
|
|
LOG((" removing %s\n", path.get()));
|
|
|
|
}
|
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
|
|
|
*/
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsOfflineCacheDeviceInfo final : public nsICacheDeviceInfo
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSICACHEDEVICEINFO
|
|
|
|
|
2014-08-05 17:20:24 +04:00
|
|
|
explicit 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:
|
2014-06-24 20:36:44 +04:00
|
|
|
~nsOfflineCacheDeviceInfo() {}
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDevice* mDevice;
|
2004-10-20 09:00:23 +04:00
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(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
|
|
|
{
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString 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
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineCacheDeviceInfo::GetEntryCount(uint32_t *aEntryCount)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
*aEntryCount = mDevice->EntryCount();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineCacheDeviceInfo::GetTotalSize(uint32_t *aTotalSize)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
*aTotalSize = mDevice->CacheSize();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineCacheDeviceInfo::GetMaximumSize(uint32_t *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
|
|
|
*/
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsOfflineCacheBinding final : public nsISupports
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2014-06-24 20:36:44 +04:00
|
|
|
~nsOfflineCacheBinding() {}
|
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
public:
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2004-10-20 09:00:23 +04:00
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_IMPL_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)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString keyBuf;
|
2007-07-25 10:31:27 +04:00
|
|
|
const char *cid, *key;
|
|
|
|
if (!DecomposeCacheEntryKey(fullKey, &cid, &key, keyBuf))
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2007-07-25 10:31:27 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint64_t hash = DCacheHash(key);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t dir1 = (uint32_t) (hash & 0x0F);
|
|
|
|
uint32_t dir2 = (uint32_t)((hash & 0xF0) >> 4);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
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))
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2004-10-20 09:00:23 +04:00
|
|
|
rv = file->Create(nsIFile::NORMAL_FILE_TYPE, 00600);
|
|
|
|
if (NS_FAILED(rv) && rv != NS_ERROR_FILE_ALREADY_EXISTS)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2004-10-20 09:00:23 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PR_snprintf(leaf, sizeof(leaf), "%014llX-%X", hash, generation);
|
|
|
|
rv = file->AppendNative(nsDependentCString(leaf));
|
|
|
|
if (NS_FAILED(rv))
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2004-10-20 09:00:23 +04:00
|
|
|
}
|
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheBinding *binding = new nsOfflineCacheBinding;
|
2004-10-20 09:00:23 +04:00
|
|
|
if (!binding)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
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;
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint8_t *metaData;
|
|
|
|
uint32_t metaDataLen;
|
|
|
|
int32_t generation;
|
|
|
|
int32_t dataSize;
|
|
|
|
int32_t fetchCount;
|
|
|
|
int64_t lastFetched;
|
|
|
|
int64_t lastModified;
|
|
|
|
int64_t expirationTime;
|
2004-10-20 09:00:23 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
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)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-29 02:27:03 +04:00
|
|
|
}
|
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))
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
entry->SetFetchCount((uint32_t) rec.fetchCount);
|
2004-10-20 09:00:23 +04:00
|
|
|
entry->SetLastFetched(SecondsFromPRTime(rec.lastFetched));
|
|
|
|
entry->SetLastModified(SecondsFromPRTime(rec.lastModified));
|
|
|
|
entry->SetExpirationTime(SecondsFromPRTime(rec.expirationTime));
|
2012-08-22 19:56:38 +04:00
|
|
|
entry->SetDataSize((uint32_t) rec.dataSize);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
entry->UnflattenMetaData((const char *) rec.metaData, rec.metaDataLen);
|
|
|
|
|
2013-01-30 01:05:43 +04:00
|
|
|
// Restore security info, if present
|
|
|
|
const char* info = entry->GetMetaDataElement("security-info");
|
|
|
|
if (info) {
|
|
|
|
nsCOMPtr<nsISupports> infoObj;
|
|
|
|
rv = NS_DeserializeObject(nsDependentCString(info),
|
|
|
|
getter_AddRefs(infoObj));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete entry;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
entry->SetSecurityInfo(infoObj);
|
|
|
|
}
|
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
// 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;
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2004-10-20 09:00:23 +04:00
|
|
|
}
|
|
|
|
entry->SetData(binding);
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
2007-05-15 00:09:20 +04:00
|
|
|
* nsOfflineCacheEntryInfo
|
2004-10-20 09:00:23 +04:00
|
|
|
*/
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsOfflineCacheEntryInfo final : public nsICacheEntryInfo
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
2014-06-24 20:36:44 +04:00
|
|
|
~nsOfflineCacheEntryInfo() {}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(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
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineCacheEntryInfo::GetFetchCount(int32_t *aFetchCount)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
*aFetchCount = mRec->fetchCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineCacheEntryInfo::GetLastFetched(uint32_t *aLastFetched)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
*aLastFetched = SecondsFromPRTime(mRec->lastFetched);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineCacheEntryInfo::GetLastModified(uint32_t *aLastModified)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
*aLastModified = SecondsFromPRTime(mRec->lastModified);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineCacheEntryInfo::GetExpirationTime(uint32_t *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
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineCacheEntryInfo::GetDataSize(uint32_t *aDataSize)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
*aDataSize = mRec->dataSize;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-30 22:34:23 +04:00
|
|
|
/******************************************************************************
|
|
|
|
* nsApplicationCacheNamespace
|
|
|
|
*/
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsApplicationCacheNamespace, nsIApplicationCacheNamespace)
|
2008-09-30 22:34:23 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsApplicationCacheNamespace::Init(uint32_t itemType,
|
2008-09-30 22:34:23 +04:00
|
|
|
const nsACString &namespaceSpec,
|
|
|
|
const nsACString &data)
|
|
|
|
{
|
|
|
|
mItemType = itemType;
|
|
|
|
mNamespaceSpec = namespaceSpec;
|
|
|
|
mData = data;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsApplicationCacheNamespace::GetItemType(uint32_t *out)
|
2008-09-30 22:34:23 +04:00
|
|
|
{
|
|
|
|
*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
|
|
|
|
*/
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsApplicationCache,
|
|
|
|
nsIApplicationCache,
|
|
|
|
nsISupportsWeakReference)
|
2008-08-27 03:09:02 +04:00
|
|
|
|
2010-10-20 21:12:32 +04:00
|
|
|
nsApplicationCache::nsApplicationCache()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mDevice(nullptr)
|
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;
|
|
|
|
|
2013-08-15 14:42:02 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mDevice->mLock);
|
|
|
|
mDevice->mCaches.Remove(mClientID);
|
|
|
|
}
|
2008-08-27 03:09:02 +04:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2012-07-31 10:36:00 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsApplicationCache::GetManifestURI(nsIURI **out)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), mGroup);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = uri->CloneIgnoringRef(out);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
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
|
2012-07-30 18:20:58 +04:00
|
|
|
*out = nullptr;
|
2012-06-04 18:12:24 +04:00
|
|
|
|
|
|
|
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))
|
2012-07-30 18:20:58 +04:00
|
|
|
mDevice = nullptr;
|
2012-07-11 22:20:17 +04:00
|
|
|
|
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
|
|
|
|
2015-03-17 19:29:17 +03:00
|
|
|
nsCOMPtr<nsIRunnable> ev =
|
2012-07-11 10:16:00 +04:00
|
|
|
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,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t typeBits)
|
2008-08-27 03:09:02 +04:00
|
|
|
{
|
|
|
|
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,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t typeBits)
|
2008-08-27 03:09:02 +04:00
|
|
|
{
|
|
|
|
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,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t *typeBits)
|
2008-08-27 03:09:02 +04:00
|
|
|
{
|
|
|
|
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
|
2012-08-22 19:56:38 +04:00
|
|
|
nsApplicationCache::GatherEntries(uint32_t typeBits,
|
|
|
|
uint32_t * count,
|
2008-08-27 03:09:02 +04:00
|
|
|
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
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t length;
|
2008-09-30 22:34:23 +04:00
|
|
|
nsresult rv = namespaces->GetLength(&length);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < length; i++) {
|
2008-09-30 22:34:23 +04:00
|
|
|
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
|
2012-08-22 19:56:38 +04:00
|
|
|
nsApplicationCache::GetUsage(uint32_t *usage)
|
2009-02-22 22:09:10 +03:00
|
|
|
{
|
|
|
|
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:
|
2014-08-05 17:20:24 +04:00
|
|
|
explicit nsCloseDBEvent(mozIStorageConnection *aDB)
|
2010-08-24 05:06:23 +04:00
|
|
|
{
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2013-07-19 06:24:13 +04:00
|
|
|
NS_IMPL_ISUPPORTS0(nsOfflineCacheDevice)
|
2008-08-27 03:09:02 +04:00
|
|
|
|
2007-05-15 00:09:20 +04:00
|
|
|
nsOfflineCacheDevice::nsOfflineCacheDevice()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mDB(nullptr)
|
2004-10-20 09:00:23 +04:00
|
|
|
, mCacheCapacity(0)
|
|
|
|
, mDeltaCounter(0)
|
2012-07-11 22:20:17 +04:00
|
|
|
, mAutoShutdown(false)
|
2013-08-15 14:42:02 +04:00
|
|
|
, mLock("nsOfflineCacheDevice.lock")
|
2014-08-06 17:31:21 +04:00
|
|
|
, mActiveCaches(4)
|
|
|
|
, mLockedEntries(32)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-24 20:36:44 +04:00
|
|
|
nsOfflineCacheDevice::~nsOfflineCacheDevice()
|
|
|
|
{}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
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);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
return (uint32_t) statement->AsInt32(0);
|
2004-10-20 09:00:23 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
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);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
return (uint32_t) statement->AsInt32(0);
|
2004-10-20 09:00:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString keyBuf;
|
2004-10-20 09:00:23 +04:00
|
|
|
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;
|
|
|
|
|
2013-01-30 01:05:43 +04:00
|
|
|
// Store security info, if it is serializable
|
|
|
|
nsCOMPtr<nsISupports> infoObj = entry->SecurityInfo();
|
|
|
|
nsCOMPtr<nsISerializable> serializable = do_QueryInterface(infoObj);
|
|
|
|
if (infoObj && !serializable)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
if (serializable) {
|
|
|
|
nsCString info;
|
|
|
|
nsresult rv = NS_SerializeToString(serializable, info);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = entry->SetMetaDataElement("security-info", info.get());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2004-10-20 09:00:23 +04:00
|
|
|
nsCString metaDataBuf;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mdSize = entry->MetaDataSize();
|
2015-01-28 12:00:40 +03:00
|
|
|
if (!metaDataBuf.SetLength(mdSize, fallible))
|
2006-06-22 23:07:30 +04:00
|
|
|
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;
|
2012-08-22 19:56:38 +04:00
|
|
|
rec.metaData = (const uint8_t *) md;
|
2004-10-20 09:00:23 +04:00
|
|
|
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;
|
2012-07-27 18:03:08 +04:00
|
|
|
rv = statement->BindBlobByIndex(0, rec.metaData, rec.metaDataLen);
|
|
|
|
nsresult tmp = statement->BindInt32ByIndex(1, rec.dataSize);
|
|
|
|
if (NS_FAILED(tmp)) {
|
|
|
|
rv = tmp;
|
|
|
|
}
|
|
|
|
tmp = statement->BindInt32ByIndex(2, rec.fetchCount);
|
|
|
|
if (NS_FAILED(tmp)) {
|
|
|
|
rv = tmp;
|
|
|
|
}
|
|
|
|
tmp = statement->BindInt64ByIndex(3, rec.lastFetched);
|
|
|
|
if (NS_FAILED(tmp)) {
|
|
|
|
rv = tmp;
|
|
|
|
}
|
|
|
|
tmp = statement->BindInt64ByIndex(4, rec.lastModified);
|
|
|
|
if (NS_FAILED(tmp)) {
|
|
|
|
rv = tmp;
|
|
|
|
}
|
|
|
|
tmp = statement->BindInt64ByIndex(5, rec.expirationTime);
|
|
|
|
if (NS_FAILED(tmp)) {
|
|
|
|
rv = tmp;
|
|
|
|
}
|
|
|
|
tmp = statement->BindUTF8StringByIndex(6, nsDependentCString(cid));
|
|
|
|
if (NS_FAILED(tmp)) {
|
|
|
|
rv = tmp;
|
|
|
|
}
|
|
|
|
tmp = statement->BindUTF8StringByIndex(7, nsDependentCString(key));
|
|
|
|
if (NS_FAILED(tmp)) {
|
|
|
|
rv = tmp;
|
|
|
|
}
|
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
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineCacheDevice::UpdateEntrySize(nsCacheEntry *entry, uint32_t newSize)
|
2004-10-20 09:00:23 +04:00
|
|
|
{
|
|
|
|
// Decompose the key into "ClientID" and "Key"
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString keyBuf;
|
2004-10-20 09:00:23 +04:00
|
|
|
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
|
|
|
|
2012-07-27 18:03:08 +04:00
|
|
|
nsresult rv = statement->BindInt32ByIndex(0, newSize);
|
|
|
|
nsresult tmp = statement->BindUTF8StringByIndex(1, nsDependentCString(cid));
|
|
|
|
if (NS_FAILED(tmp)) {
|
|
|
|
rv = tmp;
|
|
|
|
}
|
|
|
|
tmp = statement->BindUTF8StringByIndex(2, nsDependentCString(key));
|
|
|
|
if (NS_FAILED(tmp)) {
|
|
|
|
rv = tmp;
|
|
|
|
}
|
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"
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString keyBuf;
|
2004-10-20 09:00:23 +04:00
|
|
|
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
|
|
|
|
2012-07-27 18:03:08 +04:00
|
|
|
nsresult rv = statement->BindUTF8StringByIndex(0, nsDependentCString(cid));
|
|
|
|
nsresult rv2 = statement->BindUTF8StringByIndex(1, nsDependentCString(key));
|
2004-10-20 09:00:23 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-07-27 18:03:08 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv2, rv2);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
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()
|
2013-04-10 04:21:07 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(false, "Need to be initialized with sqlite");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsOfflineCacheDevice::InitWithSqlite(mozIStorageService * ss)
|
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);
|
|
|
|
|
2013-04-10 04:21:07 +04:00
|
|
|
MOZ_ASSERT(ss, "nsOfflineCacheDevice::InitWithSqlite called before nsCacheService::Init() ?");
|
|
|
|
NS_ENSURE_TRUE(ss, NS_ERROR_UNEXPECTED);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
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-10-03 04:39:09 +04:00
|
|
|
StatementSql ( mStatement_EnumerateApps, "SELECT GroupID, ActiveClientID FROM moz_cache_groups WHERE GroupID LIKE ?1;"),
|
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
|
|
|
};
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t 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;
|
|
|
|
}
|
|
|
|
|
2012-07-31 10:36:00 +04:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
GetGroupForCache(const nsCSubstring &clientID, nsCString &group)
|
|
|
|
{
|
|
|
|
group.Assign(clientID);
|
|
|
|
group.Truncate(group.FindChar('|'));
|
|
|
|
NS_UnescapeURL(group);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-11 19:13:09 +03:00
|
|
|
void
|
|
|
|
AppendJARIdentifier(nsACString &_result, OriginAttributes const *aOriginAttributes)
|
2012-07-31 10:36:00 +04:00
|
|
|
{
|
2015-10-11 19:13:09 +03:00
|
|
|
nsAutoCString suffix;
|
|
|
|
aOriginAttributes->CreateSuffix(suffix);
|
|
|
|
_result.Append(suffix);
|
2012-07-31 10:36:00 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2012-07-31 10:36:00 +04:00
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOfflineCacheDevice::BuildApplicationCacheGroupID(nsIURI *aManifestURL,
|
2015-10-11 19:13:09 +03:00
|
|
|
OriginAttributes const *aOriginAttributes,
|
2012-07-31 10:36:00 +04:00
|
|
|
nsACString &_result)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> newURI;
|
|
|
|
nsresult rv = aManifestURL->CloneIgnoringRef(getter_AddRefs(newURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoCString manifestSpec;
|
|
|
|
rv = newURI->GetAsciiSpec(manifestSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
_result.Assign(manifestSpec);
|
|
|
|
|
2015-10-11 19:13:09 +03:00
|
|
|
if (aOriginAttributes) {
|
|
|
|
AppendJARIdentifier(_result, aOriginAttributes);
|
|
|
|
}
|
2012-07-31 10:36:00 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
nsresult
|
|
|
|
nsOfflineCacheDevice::InitActiveCaches()
|
|
|
|
{
|
2013-08-15 14:42:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
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)
|
|
|
|
{
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString group;
|
2008-08-27 03:09:02 +04:00
|
|
|
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);
|
|
|
|
|
2013-08-15 14:42:02 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
2013-09-02 12:41:57 +04:00
|
|
|
mCaches.EnumerateRead(ShutdownApplicationCache, this);
|
2013-08-15 14:42:02 +04:00
|
|
|
}
|
2008-08-27 03:09:02 +04:00
|
|
|
|
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;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mStatement_CacheSize = nullptr;
|
|
|
|
mStatement_ApplicationCacheSize = nullptr;
|
|
|
|
mStatement_EntryCount = nullptr;
|
|
|
|
mStatement_UpdateEntry = nullptr;
|
|
|
|
mStatement_UpdateEntrySize = nullptr;
|
|
|
|
mStatement_DeleteEntry = nullptr;
|
|
|
|
mStatement_FindEntry = nullptr;
|
|
|
|
mStatement_BindEntry = nullptr;
|
|
|
|
mStatement_ClearDomain = nullptr;
|
|
|
|
mStatement_MarkEntry = nullptr;
|
|
|
|
mStatement_UnmarkEntry = nullptr;
|
|
|
|
mStatement_GetTypes = nullptr;
|
|
|
|
mStatement_FindNamespaceEntry = nullptr;
|
|
|
|
mStatement_InsertNamespaceEntry = nullptr;
|
|
|
|
mStatement_CleanupUnmarked = nullptr;
|
|
|
|
mStatement_GatherEntries = nullptr;
|
|
|
|
mStatement_ActivateClient = nullptr;
|
|
|
|
mStatement_DeactivateGroup = nullptr;
|
|
|
|
mStatement_FindClient = nullptr;
|
|
|
|
mStatement_FindClientByNamespace = nullptr;
|
2012-10-03 04:39:09 +04:00
|
|
|
mStatement_EnumerateApps = nullptr;
|
2012-07-30 18:20:58 +04:00
|
|
|
mStatement_EnumerateGroups = nullptr;
|
|
|
|
mStatement_EnumerateGroupsTimeOrder = nullptr;
|
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();
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mDB = nullptr;
|
|
|
|
mInitThread = nullptr;
|
2010-08-24 05:06:23 +04:00
|
|
|
|
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
|
|
|
{
|
2012-08-23 21:23:03 +04:00
|
|
|
mozilla::Telemetry::AutoTimer<mozilla::Telemetry::CACHE_OFFLINE_SEARCH_2> 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"
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString keyBuf;
|
2004-10-20 09:00:23 +04:00
|
|
|
const char *cid, *key;
|
|
|
|
if (!DecomposeCacheEntryKey(fullKey, &cid, &key, keyBuf))
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2004-10-28 02:19:32 +04:00
|
|
|
AutoResetStatement statement(mStatement_FindEntry);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
2012-07-27 18:03:08 +04:00
|
|
|
nsresult rv = statement->BindUTF8StringByIndex(0, nsDependentCString(cid));
|
|
|
|
nsresult rv2 = statement->BindUTF8StringByIndex(1, nsDependentCString(key));
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2012-07-30 18:28:15 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv2, nullptr);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
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)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr; // 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,
|
2012-08-22 19:56:38 +04:00
|
|
|
(const uint8_t **) &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;
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2007-05-15 00:09:20 +04:00
|
|
|
}
|
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"
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString keyBuf;
|
2004-10-20 09:00:23 +04:00
|
|
|
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;
|
2013-09-19 23:28:26 +04:00
|
|
|
rec.metaData = nullptr; // don't write any metadata now.
|
2004-10-20 09:00:23 +04:00
|
|
|
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
|
|
|
|
2012-07-27 18:03:08 +04:00
|
|
|
nsresult rv = statement->BindUTF8StringByIndex(0, nsDependentCString(rec.clientID));
|
|
|
|
nsresult tmp = statement->BindUTF8StringByIndex(1, nsDependentCString(rec.key));
|
|
|
|
if (NS_FAILED(tmp)) {
|
|
|
|
rv = tmp;
|
|
|
|
}
|
|
|
|
tmp = statement->BindBlobByIndex(2, rec.metaData, rec.metaDataLen);
|
|
|
|
if (NS_FAILED(tmp)) {
|
|
|
|
rv = tmp;
|
|
|
|
}
|
|
|
|
tmp = statement->BindInt32ByIndex(3, rec.generation);
|
|
|
|
if (NS_FAILED(tmp)) {
|
|
|
|
rv = tmp;
|
|
|
|
}
|
|
|
|
tmp = statement->BindInt32ByIndex(4, rec.dataSize);
|
|
|
|
if (NS_FAILED(tmp)) {
|
|
|
|
rv = tmp;
|
|
|
|
}
|
|
|
|
tmp = statement->BindInt32ByIndex(5, rec.fetchCount);
|
|
|
|
if (NS_FAILED(tmp)) {
|
|
|
|
rv = tmp;
|
|
|
|
}
|
|
|
|
tmp = statement->BindInt64ByIndex(6, rec.lastFetched);
|
|
|
|
if (NS_FAILED(tmp)) {
|
|
|
|
rv = tmp;
|
|
|
|
}
|
|
|
|
tmp = statement->BindInt64ByIndex(7, rec.lastModified);
|
|
|
|
if (NS_FAILED(tmp)) {
|
|
|
|
rv = tmp;
|
|
|
|
}
|
|
|
|
tmp = statement->BindInt64ByIndex(8, rec.expirationTime);
|
|
|
|
if (NS_FAILED(tmp)) {
|
|
|
|
rv = tmp;
|
|
|
|
}
|
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,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t offset,
|
2007-05-15 00:09:20 +04:00
|
|
|
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
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*result = nullptr;
|
2004-10-20 09:00:23 +04:00
|
|
|
|
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,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t offset,
|
2007-05-15 00:09:20 +04:00
|
|
|
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
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*result = nullptr;
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
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;
|
2012-10-16 12:38:20 +04:00
|
|
|
nsresult rv =
|
|
|
|
NS_NewBufferedOutputStream(getter_AddRefs(bufferedOut), out, 16 * 1024);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2004-10-20 09:00:23 +04:00
|
|
|
|
|
|
|
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
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineCacheDevice::OnDataSizeChange(nsCacheEntry *entry, int32_t 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));
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
const int32_t DELTA_THRESHOLD = 1<<14; // 16k
|
2007-05-15 00:09:20 +04:00
|
|
|
|
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
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t oldSize = entry->DataSize();
|
|
|
|
NS_ASSERTION(deltaSize >= 0 || int32_t(oldSize) + deltaSize >= 0, "oops");
|
|
|
|
uint32_t newSize = int32_t(oldSize) + deltaSize;
|
2004-10-20 09:00:23 +04:00
|
|
|
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;
|
|
|
|
|
2013-09-19 23:28:26 +04:00
|
|
|
statement->GetSharedUTF8String(0, nullptr, &rec.clientID);
|
|
|
|
statement->GetSharedUTF8String(1, nullptr, &rec.key);
|
2007-05-15 00:09:20 +04:00
|
|
|
statement->GetSharedBlob(2, &rec.metaDataLen,
|
2012-08-22 19:56:38 +04:00
|
|
|
(const uint8_t **) &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;
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
info->mRec = nullptr;
|
2004-10-20 09:00:23 +04:00
|
|
|
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-07-30 18:20:58 +04:00
|
|
|
statement = nullptr;
|
2012-04-01 02:18:08 +04:00
|
|
|
// 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,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t typeBits)
|
2008-08-27 03:09:02 +04:00
|
|
|
{
|
|
|
|
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,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t typeBits)
|
2008-08-27 03:09:02 +04:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*out = nullptr;
|
2008-09-30 22:34:23 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool found = false;
|
2009-02-22 22:09:10 +03:00
|
|
|
nsCString nsSpec;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t nsType = 0;
|
2009-02-22 22:09:10 +03:00
|
|
|
nsCString nsData;
|
|
|
|
|
|
|
|
while (hasRows)
|
2008-09-30 22:34:23 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t itemType;
|
2009-02-22 22:09:10 +03:00
|
|
|
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,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t *typeBits)
|
2008-08-27 03:09:02 +04:00
|
|
|
{
|
|
|
|
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,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t typeBits,
|
|
|
|
uint32_t *count,
|
2008-08-27 03:09:02 +04:00
|
|
|
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);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t itemType;
|
2008-09-30 22:34:23 +04:00
|
|
|
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,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t *usage)
|
2009-02-22 22:09:10 +03:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
*usage = static_cast<uint32_t>(statement->AsInt32(0));
|
2009-02-22 22:09:10 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-04-30 21:57:09 +04:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineCacheDevice::GetGroups(uint32_t *count,
|
2009-02-22 22:09:10 +03:00
|
|
|
char ***keys)
|
|
|
|
{
|
|
|
|
LOG(("nsOfflineCacheDevice::GetGroups"));
|
|
|
|
|
|
|
|
return RunSimpleQuery(mStatement_EnumerateGroups, 0, count, keys);
|
|
|
|
}
|
|
|
|
|
2012-04-30 21:57:09 +04:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineCacheDevice::GetGroupsTimeOrdered(uint32_t *count,
|
2012-03-31 04:52:06 +04:00
|
|
|
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)
|
|
|
|
{
|
2013-08-15 14:42:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2012-06-29 02:27:03 +04:00
|
|
|
return mLockedEntries.GetEntry(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsOfflineCacheDevice::Lock(const nsACString &key)
|
|
|
|
{
|
2013-08-15 14:42:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2012-06-29 02:27:03 +04:00
|
|
|
mLockedEntries.PutEntry(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsOfflineCacheDevice::Unlock(const nsACString &key)
|
|
|
|
{
|
2013-08-15 14:42:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2012-06-29 02:27:03 +04:00
|
|
|
mLockedEntries.RemoveEntry(key);
|
|
|
|
}
|
|
|
|
|
2007-07-25 09:35:39 +04:00
|
|
|
nsresult
|
|
|
|
nsOfflineCacheDevice::RunSimpleQuery(mozIStorageStatement * statement,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t resultIndex,
|
|
|
|
uint32_t * count,
|
2007-07-25 09:35:39 +04:00
|
|
|
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)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t length;
|
2007-07-25 09:35:39 +04:00
|
|
|
valArray.AppendElement(
|
|
|
|
nsDependentCString(statement->AsSharedUTF8String(resultIndex, &length)));
|
|
|
|
|
|
|
|
rv = statement->ExecuteStep(&hasRows);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
*count = valArray.Length();
|
2015-04-01 08:29:55 +03:00
|
|
|
char **ret = static_cast<char **>(moz_xmalloc(*count * sizeof(char*)));
|
2007-07-25 09:35:39 +04:00
|
|
|
if (!ret) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < *count; i++) {
|
2007-07-25 09:35:39 +04:00
|
|
|
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
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*out = nullptr;
|
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
|
|
|
|
2013-08-15 14:42:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
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)
|
2013-08-15 14:42:02 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
return GetApplicationCache_Unlocked(clientID, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsOfflineCacheDevice::GetApplicationCache_Unlocked(const nsACString &clientID,
|
|
|
|
nsIApplicationCache **out)
|
2008-08-25 10:11:31 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*out = nullptr;
|
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
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*out = nullptr;
|
2008-08-20 09:53:20 +04:00
|
|
|
|
2013-08-15 14:42:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
2008-08-27 03:09:02 +04:00
|
|
|
nsCString *clientID;
|
|
|
|
if (mActiveCachesByGroup.Get(group, &clientID))
|
2013-08-15 14:42:02 +04:00
|
|
|
return GetApplicationCache_Unlocked(*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)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
nsCString *active = nullptr;
|
2008-11-04 21:59:12 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2013-08-15 14:42:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
2008-11-04 21:59:12 +03:00
|
|
|
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);
|
2012-07-30 18:20:58 +04:00
|
|
|
active = nullptr;
|
2008-11-04 21:59:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-03 04:39:09 +04:00
|
|
|
nsresult
|
|
|
|
nsOfflineCacheDevice::DiscardByAppId(int32_t appID, bool browserEntriesOnly)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsAutoCString jaridsuffix;
|
2015-10-11 19:13:09 +03:00
|
|
|
|
2012-10-03 04:39:09 +04:00
|
|
|
jaridsuffix.Append('%');
|
2015-10-11 19:13:09 +03:00
|
|
|
|
|
|
|
// TODO - this method should accept OriginAttributes* from outside instead.
|
|
|
|
// If passed null, we should then delegate to
|
|
|
|
// nsCacheService::GlobalInstance()->EvictEntriesInternal(nsICache::STORE_OFFLINE);
|
|
|
|
|
|
|
|
OriginAttributes oa;
|
|
|
|
oa.mAppId = appID;
|
|
|
|
oa.mInBrowser = browserEntriesOnly;
|
|
|
|
AppendJARIdentifier(jaridsuffix, &oa);
|
2012-10-03 04:39:09 +04:00
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
{
|
|
|
|
AutoResetStatement statement(mStatement_EnumerateApps);
|
|
|
|
rv = statement->BindUTF8StringByIndex(0, jaridsuffix);
|
2012-10-03 04:39:09 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
bool hasRows;
|
|
|
|
rv = statement->ExecuteStep(&hasRows);
|
2012-10-03 04:39:09 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
while (hasRows) {
|
|
|
|
nsAutoCString group;
|
|
|
|
rv = statement->GetUTF8String(0, group);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-10-03 04:39:09 +04:00
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
nsCString clientID;
|
|
|
|
rv = statement->GetUTF8String(1, clientID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-10-03 04:39:09 +04:00
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
nsCOMPtr<nsIRunnable> ev =
|
|
|
|
new nsOfflineCacheDiscardCache(this, group, clientID);
|
|
|
|
|
|
|
|
rv = nsCacheService::DispatchToCacheIOThread(ev);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = statement->ExecuteStep(&hasRows);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2012-10-03 04:39:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!browserEntriesOnly) {
|
|
|
|
// If deleting app, delete any 'inBrowserElement' entries too
|
|
|
|
rv = DiscardByAppId(appID, true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-07-31 10:36:00 +04:00
|
|
|
nsOfflineCacheDevice::CanUseCache(nsIURI *keyURI,
|
|
|
|
const nsACString &clientID,
|
2013-09-20 13:11:25 +04:00
|
|
|
nsILoadContextInfo *loadContextInfo)
|
2012-07-31 10:36:00 +04:00
|
|
|
{
|
2013-08-15 14:42:02 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
if (!mActiveCaches.Contains(clientID))
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-30 22:34:23 +04:00
|
|
|
|
2012-07-31 10:36:00 +04:00
|
|
|
nsAutoCString groupID;
|
|
|
|
nsresult rv = GetGroupForCache(clientID, groupID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> groupURI;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(groupURI), groupID);
|
2015-10-11 19:13:09 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-31 10:36:00 +04:00
|
|
|
return false;
|
2015-10-11 19:13:09 +03:00
|
|
|
}
|
2012-07-31 10:36:00 +04:00
|
|
|
|
|
|
|
// 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,
|
2015-10-11 19:13:09 +03:00
|
|
|
GetStrictFileOriginPolicy())) {
|
2012-07-31 10:36:00 +04:00
|
|
|
return false;
|
2012-10-02 17:55:00 +04:00
|
|
|
}
|
|
|
|
|
2012-07-31 10:36:00 +04:00
|
|
|
// Check the groupID we found is equal to groupID based
|
|
|
|
// on the load context demanding load from app cache.
|
|
|
|
// This is check of extended origin.
|
|
|
|
nsAutoCString demandedGroupID;
|
2015-10-11 19:13:09 +03:00
|
|
|
|
|
|
|
const OriginAttributes *oa = loadContextInfo
|
|
|
|
? loadContextInfo->OriginAttributesPtr()
|
|
|
|
: nullptr;
|
|
|
|
rv = BuildApplicationCacheGroupID(groupURI, oa, demandedGroupID);
|
2012-07-31 10:36:00 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
2015-10-11 19:13:09 +03:00
|
|
|
if (groupID != demandedGroupID) {
|
2012-07-31 10:36:00 +04:00
|
|
|
return false;
|
2015-10-11 19:13:09 +03:00
|
|
|
}
|
2012-07-31 10:36:00 +04:00
|
|
|
|
|
|
|
return true;
|
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,
|
2013-09-20 13:11:25 +04:00
|
|
|
nsILoadContextInfo *loadContextInfo,
|
2008-08-27 03:09:02 +04:00
|
|
|
nsIApplicationCache **out)
|
2008-08-25 10:11:31 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*out = nullptr;
|
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) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t itemType;
|
2008-08-28 05:15:32 +04:00
|
|
|
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)) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString clientID;
|
2008-08-28 05:15:32 +04:00
|
|
|
rv = statement->GetUTF8String(0, clientID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
if (CanUseCache(keyURI, clientID, loadContextInfo)) {
|
2008-09-30 22:34:23 +04:00
|
|
|
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)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t itemType;
|
2008-09-30 22:34:23 +04:00
|
|
|
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)) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString clientID;
|
2008-09-30 22:34:23 +04:00
|
|
|
rv = nsstatement->GetUTF8String(0, clientID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-09-20 13:11:25 +04:00
|
|
|
if (CanUseCache(keyURI, clientID, loadContextInfo)) {
|
2008-09-30 22:34:23 +04:00
|
|
|
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;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString clientID;
|
2008-09-30 22:34:23 +04:00
|
|
|
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);
|
|
|
|
|
2013-08-15 14:42:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
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);
|
2012-07-30 18:20:58 +04:00
|
|
|
active = nullptr;
|
2008-08-27 03:09:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
nsCString *active = nullptr;
|
2013-08-15 14:42:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2008-08-27 03:09:02 +04:00
|
|
|
return mActiveCachesByGroup.Get(group, &active) && *active == clientID;
|
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)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
mCacheDirectory = nullptr;
|
2004-10-20 09:00:23 +04:00
|
|
|
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
|
2012-08-22 19:56:38 +04:00
|
|
|
nsOfflineCacheDevice::SetCapacity(uint32_t 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);
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString clientID;
|
2012-07-11 22:20:17 +04:00
|
|
|
aAppCache->GetClientID(clientID);
|
2013-08-15 14:42:02 +04:00
|
|
|
|
|
|
|
MutexAutoLock lock(mLock);
|
2012-07-11 22:20:17 +04:00
|
|
|
mCaches.Remove(clientID);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|