2015-03-02 16:20:00 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/cache/Manager.h"
|
|
|
|
|
2015-03-07 00:39:05 +03:00
|
|
|
#include "mozilla/AutoRestore.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
|
|
|
#include "mozilla/StaticMutex.h"
|
|
|
|
#include "mozilla/StaticPtr.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "mozilla/dom/cache/Context.h"
|
|
|
|
#include "mozilla/dom/cache/DBAction.h"
|
|
|
|
#include "mozilla/dom/cache/DBSchema.h"
|
|
|
|
#include "mozilla/dom/cache/FileUtils.h"
|
|
|
|
#include "mozilla/dom/cache/ManagerId.h"
|
2015-04-16 22:00:15 +03:00
|
|
|
#include "mozilla/dom/cache/CacheTypes.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "mozilla/dom/cache/SavedTypes.h"
|
|
|
|
#include "mozilla/dom/cache/StreamList.h"
|
|
|
|
#include "mozilla/dom/cache/Types.h"
|
2017-07-18 14:00:09 +03:00
|
|
|
#include "mozilla/dom/cache/QuotaClient.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "mozilla/ipc/BackgroundParent.h"
|
|
|
|
#include "mozStorageHelper.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsID.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsIThread.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsTObserverArray.h"
|
|
|
|
|
|
|
|
|
2015-06-26 08:22:46 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace cache {
|
|
|
|
|
|
|
|
namespace {
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
// An Action that is executed when a Context is first created. It ensures that
|
|
|
|
// the directory and database are setup properly. This lets other actions
|
|
|
|
// not worry about these details.
|
2015-03-21 19:28:04 +03:00
|
|
|
class SetupAction final : public SyncDBAction
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
SetupAction()
|
|
|
|
: SyncDBAction(DBAction::Create)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
RunSyncWithDBOnTarget(const QuotaInfo& aQuotaInfo, nsIFile* aDBDir,
|
2015-03-21 19:28:04 +03:00
|
|
|
mozIStorageConnection* aConn) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2017-07-18 14:00:09 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aDBDir);
|
|
|
|
|
2015-04-16 22:00:16 +03:00
|
|
|
nsresult rv = BodyCreateDir(aDBDir);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
2015-09-01 00:26:30 +03:00
|
|
|
// executes in its own transaction
|
|
|
|
rv = db::CreateOrMigrateSchema(aConn);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
2015-06-26 08:22:46 +03:00
|
|
|
|
|
|
|
// If the Context marker file exists, then the last session was
|
|
|
|
// not cleanly shutdown. In these cases sqlite will ensure that
|
|
|
|
// the database is valid, but we might still orphan data. Both
|
|
|
|
// Cache objects and body files can be referenced by DOM objects
|
|
|
|
// after they are "removed" from their parent. So we need to
|
|
|
|
// look and see if any of these late access objects have been
|
|
|
|
// orphaned.
|
|
|
|
//
|
|
|
|
// Note, this must be done after any schema version updates to
|
|
|
|
// ensure our DBSchema methods work correctly.
|
|
|
|
if (MarkerFileExists(aQuotaInfo)) {
|
|
|
|
NS_WARNING("Cache not shutdown cleanly! Cleaning up stale data...");
|
|
|
|
mozStorageTransaction trans(aConn, false,
|
|
|
|
mozIStorageConnection::TRANSACTION_IMMEDIATE);
|
|
|
|
|
|
|
|
// Clean up orphaned Cache objects
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<CacheId, 8> orphanedCacheIdList;
|
2015-06-26 08:22:46 +03:00
|
|
|
nsresult rv = db::FindOrphanedCacheIds(aConn, orphanedCacheIdList);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
2017-07-18 14:00:09 +03:00
|
|
|
int64_t overallDeletedPaddingSize = 0;
|
2015-06-26 08:22:46 +03:00
|
|
|
for (uint32_t i = 0; i < orphanedCacheIdList.Length(); ++i) {
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<nsID, 16> deletedBodyIdList;
|
2017-07-10 11:54:20 +03:00
|
|
|
int64_t deletedPaddingSize = 0;
|
|
|
|
rv = db::DeleteCacheId(aConn, orphanedCacheIdList[i], deletedBodyIdList,
|
|
|
|
&deletedPaddingSize);
|
2015-06-26 08:22:46 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
2017-05-23 13:33:47 +03:00
|
|
|
rv = BodyDeleteFiles(aQuotaInfo, aDBDir, deletedBodyIdList);
|
2015-06-26 08:22:46 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
2017-07-10 12:02:44 +03:00
|
|
|
|
|
|
|
if (deletedPaddingSize > 0) {
|
|
|
|
DecreaseUsageForQuotaInfo(aQuotaInfo, deletedPaddingSize);
|
|
|
|
}
|
2017-07-18 14:00:09 +03:00
|
|
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(INT64_MAX - deletedPaddingSize >=
|
|
|
|
overallDeletedPaddingSize);
|
|
|
|
overallDeletedPaddingSize += deletedPaddingSize;
|
2015-06-26 08:22:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up orphaned body objects
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<nsID, 64> knownBodyIdList;
|
2015-06-26 08:22:46 +03:00
|
|
|
rv = db::GetKnownBodyIds(aConn, knownBodyIdList);
|
|
|
|
|
2017-05-23 13:33:47 +03:00
|
|
|
rv = BodyDeleteOrphanedFiles(aQuotaInfo, aDBDir, knownBodyIdList);
|
2015-06-26 08:22:46 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
2017-07-18 14:00:09 +03:00
|
|
|
|
|
|
|
// Commit() explicitly here, because we want to ensure the padding file
|
|
|
|
// has the correct content.
|
|
|
|
rv = MaybeUpdatePaddingFile(aDBDir, aConn, /* aIncreaceSize */ 0,
|
|
|
|
overallDeletedPaddingSize,
|
|
|
|
[&trans]() mutable { return trans.Commit(); });
|
|
|
|
// We'll restore padding file below, so just warn here if failure happens.
|
|
|
|
Unused << NS_WARN_IF(NS_FAILED(rv));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DirectoryPaddingFileExists(aDBDir, DirPaddingFile::TMP_FILE) ||
|
|
|
|
!DirectoryPaddingFileExists(aDBDir, DirPaddingFile::FILE)) {
|
|
|
|
rv = RestorePaddingFile(aDBDir, aConn);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
2015-06-26 08:22:46 +03:00
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Action that is executed when we determine that content has stopped using
|
|
|
|
// a body file that has been orphaned.
|
2015-03-21 19:28:04 +03:00
|
|
|
class DeleteOrphanedBodyAction final : public Action
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit DeleteOrphanedBodyAction(const nsTArray<nsID>& aDeletedBodyIdList)
|
|
|
|
: mDeletedBodyIdList(aDeletedBodyIdList)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
explicit DeleteOrphanedBodyAction(const nsID& aBodyId)
|
|
|
|
{
|
|
|
|
mDeletedBodyIdList.AppendElement(aBodyId);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
2015-05-07 15:16:51 +03:00
|
|
|
RunOnTarget(Resolver* aResolver, const QuotaInfo& aQuotaInfo, Data*) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aResolver);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aQuotaInfo.mDir);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-18 16:19:00 +03:00
|
|
|
// Note that since DeleteOrphanedBodyAction isn't used while the context is
|
|
|
|
// being initialized, we don't need to check for cancellation here.
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> dbDir;
|
|
|
|
nsresult rv = aQuotaInfo.mDir->Clone(getter_AddRefs(dbDir));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aResolver->Resolve(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = dbDir->Append(NS_LITERAL_STRING("cache"));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aResolver->Resolve(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-23 13:33:47 +03:00
|
|
|
rv = BodyDeleteFiles(aQuotaInfo, dbDir, mDeletedBodyIdList);
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << NS_WARN_IF(NS_FAILED(rv));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
aResolver->Resolve(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsTArray<nsID> mDeletedBodyIdList;
|
|
|
|
};
|
|
|
|
|
2016-02-17 11:17:20 +03:00
|
|
|
bool IsHeadRequest(const CacheRequest& aRequest, const CacheQueryParams& aParams)
|
2015-04-23 16:17:37 +03:00
|
|
|
{
|
|
|
|
return !aParams.ignoreMethod() && aRequest.method().LowerCaseEqualsLiteral("head");
|
|
|
|
}
|
|
|
|
|
2016-02-17 11:17:20 +03:00
|
|
|
bool IsHeadRequest(const CacheRequestOrVoid& aRequest, const CacheQueryParams& aParams)
|
2015-04-23 16:17:37 +03:00
|
|
|
{
|
|
|
|
if (aRequest.type() == CacheRequestOrVoid::TCacheRequest) {
|
|
|
|
return !aParams.ignoreMethod() &&
|
|
|
|
aRequest.get_CacheRequest().method().LowerCaseEqualsLiteral("head");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-04-23 16:17:37 +03:00
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Singleton class to track Manager instances and ensure there is only
|
|
|
|
// one for each unique ManagerId.
|
|
|
|
class Manager::Factory
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
friend class StaticAutoPtr<Manager::Factory>;
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
GetOrCreate(ManagerId* aManagerId, Manager** aManagerOut)
|
|
|
|
{
|
|
|
|
mozilla::ipc::AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
// Ensure there is a factory instance. This forces the Get() call
|
|
|
|
// below to use the same factory.
|
|
|
|
nsresult rv = MaybeCreateInstance();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Manager> ref = Get(aManagerId);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (!ref) {
|
|
|
|
// TODO: replace this with a thread pool (bug 1119864)
|
|
|
|
nsCOMPtr<nsIThread> ioThread;
|
|
|
|
rv = NS_NewNamedThread("DOMCacheThread", getter_AddRefs(ioThread));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
|
|
|
ref = new Manager(aManagerId, ioThread);
|
2015-04-20 21:14:57 +03:00
|
|
|
|
|
|
|
// There may be an old manager for this origin in the process of
|
|
|
|
// cleaning up. We need to tell the new manager about this so
|
|
|
|
// that it won't actually start until the old manager is done.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Manager> oldManager = Get(aManagerId, Closing);
|
2015-04-20 21:14:57 +03:00
|
|
|
ref->Init(oldManager);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(!sFactory->mManagerList.Contains(ref));
|
|
|
|
sFactory->mManagerList.AppendElement(ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
ref.forget(aManagerOut);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static already_AddRefed<Manager>
|
2015-04-20 21:14:57 +03:00
|
|
|
Get(ManagerId* aManagerId, State aState = Open)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
mozilla::ipc::AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
nsresult rv = MaybeCreateInstance();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return nullptr; }
|
|
|
|
|
2015-04-20 21:14:57 +03:00
|
|
|
// Iterate in reverse to find the most recent, matching Manager. This
|
|
|
|
// is important when looking for a Closing Manager. If a new Manager
|
|
|
|
// chains to an old Manager we want it to be the most recent one.
|
|
|
|
ManagerList::BackwardIterator iter(sFactory->mManagerList);
|
2015-03-02 16:20:00 +03:00
|
|
|
while (iter.HasMore()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Manager> manager = iter.GetNext();
|
2015-04-20 21:14:57 +03:00
|
|
|
if (aState == manager->GetState() && *manager->mManagerId == *aManagerId) {
|
2015-03-02 16:20:00 +03:00
|
|
|
return manager.forget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Remove(Manager* aManager)
|
|
|
|
{
|
|
|
|
mozilla::ipc::AssertIsOnBackgroundThread();
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aManager);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(sFactory);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
MOZ_ALWAYS_TRUE(sFactory->mManagerList.RemoveElement(aManager));
|
|
|
|
|
|
|
|
// clean up the factory singleton if there are no more managers
|
2015-03-07 00:39:05 +03:00
|
|
|
MaybeDestroyInstance();
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2015-06-30 15:59:27 +03:00
|
|
|
static void
|
2015-11-22 12:44:16 +03:00
|
|
|
Abort(const nsACString& aOrigin)
|
2015-06-30 15:59:27 +03:00
|
|
|
{
|
2015-11-22 12:44:16 +03:00
|
|
|
mozilla::ipc::AssertIsOnBackgroundThread();
|
2015-06-30 15:59:27 +03:00
|
|
|
|
2015-11-22 12:44:16 +03:00
|
|
|
if (!sFactory) {
|
2015-06-30 15:59:27 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!sFactory->mManagerList.IsEmpty());
|
2015-11-22 12:44:16 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
ManagerList::ForwardIterator iter(sFactory->mManagerList);
|
|
|
|
while (iter.HasMore()) {
|
|
|
|
RefPtr<Manager> manager = iter.GetNext();
|
|
|
|
if (aOrigin.IsVoid() ||
|
|
|
|
manager->mManagerId->QuotaOrigin() == aOrigin) {
|
|
|
|
manager->Abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-30 15:59:27 +03:00
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
static void
|
2015-11-22 12:44:16 +03:00
|
|
|
ShutdownAll()
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-11-22 12:44:16 +03:00
|
|
|
mozilla::ipc::AssertIsOnBackgroundThread();
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-11-22 12:44:16 +03:00
|
|
|
if (!sFactory) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!sFactory->mManagerList.IsEmpty());
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-11-22 12:44:16 +03:00
|
|
|
{
|
|
|
|
// Note that we are synchronously calling shutdown code here. If any
|
|
|
|
// of the shutdown code synchronously decides to delete the Factory
|
|
|
|
// we need to delay that delete until the end of this method.
|
|
|
|
AutoRestore<bool> restore(sFactory->mInSyncShutdown);
|
|
|
|
sFactory->mInSyncShutdown = true;
|
|
|
|
|
|
|
|
ManagerList::ForwardIterator iter(sFactory->mManagerList);
|
|
|
|
while (iter.HasMore()) {
|
|
|
|
RefPtr<Manager> manager = iter.GetNext();
|
|
|
|
manager->Shutdown();
|
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2015-11-22 12:44:16 +03:00
|
|
|
MaybeDestroyInstance();
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2015-11-22 12:44:16 +03:00
|
|
|
IsShutdownAllComplete()
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-11-22 12:44:16 +03:00
|
|
|
mozilla::ipc::AssertIsOnBackgroundThread();
|
|
|
|
return !sFactory;
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Factory()
|
2015-03-07 00:39:05 +03:00
|
|
|
: mInSyncShutdown(false)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(cache::Manager::Factory);
|
|
|
|
}
|
|
|
|
|
|
|
|
~Factory()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(cache::Manager::Factory);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mManagerList.IsEmpty());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mInSyncShutdown);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
MaybeCreateInstance()
|
|
|
|
{
|
|
|
|
mozilla::ipc::AssertIsOnBackgroundThread();
|
|
|
|
|
|
|
|
if (!sFactory) {
|
|
|
|
// Be clear about what we are locking. sFactory is bg thread only, so
|
|
|
|
// we don't need to lock it here. Just protect sFactoryShutdown and
|
|
|
|
// sBackgroundThread.
|
|
|
|
{
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
|
|
|
|
if (sFactoryShutdown) {
|
|
|
|
return NS_ERROR_ILLEGAL_DURING_SHUTDOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We cannot use ClearOnShutdown() here because we're not on the main
|
|
|
|
// thread. Instead, we delete sFactory in Factory::Remove() after the
|
|
|
|
// last manager is removed. ShutdownObserver ensures this happens
|
|
|
|
// before shutdown.
|
|
|
|
sFactory = new Factory();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Never return sFactory to code outside Factory. We need to delete it
|
|
|
|
// out from under ourselves just before we return from Remove(). This
|
|
|
|
// would be (even more) dangerous if other code had a pointer to the
|
|
|
|
// factory itself.
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-03-07 00:39:05 +03:00
|
|
|
MaybeDestroyInstance()
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
mozilla::ipc::AssertIsOnBackgroundThread();
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(sFactory);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-07 00:39:05 +03:00
|
|
|
// If the factory is is still in use then we cannot delete yet. This
|
|
|
|
// could be due to managers still existing or because we are in the
|
|
|
|
// middle of shutting down. We need to be careful not to delete ourself
|
|
|
|
// synchronously during shutdown.
|
|
|
|
if (!sFactory->mManagerList.IsEmpty() || sFactory->mInSyncShutdown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
sFactory = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Singleton created on demand and deleted when last Manager is cleared
|
|
|
|
// in Remove().
|
|
|
|
// PBackground thread only.
|
|
|
|
static StaticAutoPtr<Factory> sFactory;
|
|
|
|
|
2015-11-22 12:44:16 +03:00
|
|
|
// protects following static attribute
|
2015-03-02 16:20:00 +03:00
|
|
|
static StaticMutex sMutex;
|
|
|
|
|
|
|
|
// Indicate if shutdown has occurred to block re-creation of sFactory.
|
|
|
|
// Must hold sMutex to access.
|
|
|
|
static bool sFactoryShutdown;
|
|
|
|
|
|
|
|
// Weak references as we don't want to keep Manager objects alive forever.
|
|
|
|
// When a Manager is destroyed it calls Factory::Remove() to clear itself.
|
|
|
|
// PBackground thread only.
|
|
|
|
typedef nsTObserverArray<Manager*> ManagerList;
|
|
|
|
ManagerList mManagerList;
|
2015-03-07 00:39:05 +03:00
|
|
|
|
|
|
|
// This flag is set when we are looping through the list and calling
|
|
|
|
// Shutdown() on each Manager. We need to be careful not to synchronously
|
|
|
|
// trigger the deletion of the factory while still executing this loop.
|
|
|
|
bool mInSyncShutdown;
|
2015-03-02 16:20:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// static
|
|
|
|
StaticAutoPtr<Manager::Factory> Manager::Factory::sFactory;
|
|
|
|
|
|
|
|
// static
|
|
|
|
StaticMutex Manager::Factory::sMutex;
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool Manager::Factory::sFactoryShutdown = false;
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Abstract class to help implement the various Actions. The vast majority
|
|
|
|
// of Actions are synchronous and need to report back to a Listener on the
|
|
|
|
// Manager.
|
|
|
|
class Manager::BaseAction : public SyncDBAction
|
|
|
|
{
|
|
|
|
protected:
|
2015-04-16 22:00:15 +03:00
|
|
|
BaseAction(Manager* aManager, ListenerId aListenerId)
|
2015-03-02 16:20:00 +03:00
|
|
|
: SyncDBAction(DBAction::Existing)
|
|
|
|
, mManager(aManager)
|
|
|
|
, mListenerId(aListenerId)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
2015-04-16 22:00:15 +03:00
|
|
|
Complete(Listener* aListener, ErrorResult&& aRv) = 0;
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
virtual void
|
2015-03-21 19:28:04 +03:00
|
|
|
CompleteOnInitiatingThread(nsresult aRv) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager::BaseAction);
|
|
|
|
Listener* listener = mManager->GetListener(mListenerId);
|
|
|
|
if (listener) {
|
2015-04-16 22:00:15 +03:00
|
|
|
Complete(listener, ErrorResult(aRv));
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ensure we release the manager on the initiating thread
|
|
|
|
mManager = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Manager> mManager;
|
2015-03-02 16:20:00 +03:00
|
|
|
const ListenerId mListenerId;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Action that is executed when we determine that content has stopped using
|
|
|
|
// a Cache object that has been orphaned.
|
2015-03-21 19:28:04 +03:00
|
|
|
class Manager::DeleteOrphanedCacheAction final : public SyncDBAction
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
DeleteOrphanedCacheAction(Manager* aManager, CacheId aCacheId)
|
|
|
|
: SyncDBAction(DBAction::Existing)
|
|
|
|
, mManager(aManager)
|
|
|
|
, mCacheId(aCacheId)
|
2017-07-10 11:54:20 +03:00
|
|
|
, mDeletedPaddingSize(0)
|
2015-03-02 16:20:00 +03:00
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
RunSyncWithDBOnTarget(const QuotaInfo& aQuotaInfo, nsIFile* aDBDir,
|
2015-03-21 19:28:04 +03:00
|
|
|
mozIStorageConnection* aConn) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2017-07-10 12:02:44 +03:00
|
|
|
mQuotaInfo.emplace(aQuotaInfo);
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
mozStorageTransaction trans(aConn, false,
|
|
|
|
mozIStorageConnection::TRANSACTION_IMMEDIATE);
|
|
|
|
|
2017-07-10 11:54:20 +03:00
|
|
|
nsresult rv = db::DeleteCacheId(aConn, mCacheId, mDeletedBodyIdList,
|
|
|
|
&mDeletedPaddingSize);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
2017-07-18 14:00:09 +03:00
|
|
|
rv = MaybeUpdatePaddingFile(aDBDir, aConn, /* aIncreaceSize */ 0,
|
|
|
|
mDeletedPaddingSize,
|
|
|
|
[&trans]() mutable { return trans.Commit(); });
|
|
|
|
Unused << NS_WARN_IF(NS_FAILED(rv));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
2015-03-21 19:28:04 +03:00
|
|
|
CompleteOnInitiatingThread(nsresult aRv) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2017-10-03 13:36:44 +03:00
|
|
|
// If the transaction fails, we shouldn't delete the body files and decrease
|
|
|
|
// their padding size.
|
|
|
|
if (NS_FAILED(aRv)) {
|
|
|
|
mDeletedBodyIdList.Clear();
|
|
|
|
mDeletedPaddingSize = 0;
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
mManager->NoteOrphanedBodyIdList(mDeletedBodyIdList);
|
|
|
|
|
2017-07-10 12:02:44 +03:00
|
|
|
if (mDeletedPaddingSize > 0) {
|
|
|
|
DecreaseUsageForQuotaInfo(mQuotaInfo.ref(), mDeletedPaddingSize);
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
// ensure we release the manager on the initiating thread
|
|
|
|
mManager = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Manager> mManager;
|
2015-03-02 16:20:00 +03:00
|
|
|
const CacheId mCacheId;
|
|
|
|
nsTArray<nsID> mDeletedBodyIdList;
|
2017-07-10 12:02:44 +03:00
|
|
|
Maybe<QuotaInfo> mQuotaInfo;
|
2017-07-10 11:54:20 +03:00
|
|
|
// Track any pad amount associated with orphaned entries.
|
|
|
|
int64_t mDeletedPaddingSize;
|
2015-03-02 16:20:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class Manager::CacheMatchAction final : public Manager::BaseAction
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CacheMatchAction(Manager* aManager, ListenerId aListenerId,
|
2015-04-16 22:00:15 +03:00
|
|
|
CacheId aCacheId, const CacheMatchArgs& aArgs,
|
2015-03-02 16:20:00 +03:00
|
|
|
StreamList* aStreamList)
|
2015-04-16 22:00:15 +03:00
|
|
|
: BaseAction(aManager, aListenerId)
|
2015-03-02 16:20:00 +03:00
|
|
|
, mCacheId(aCacheId)
|
2015-04-16 22:00:15 +03:00
|
|
|
, mArgs(aArgs)
|
2015-03-02 16:20:00 +03:00
|
|
|
, mStreamList(aStreamList)
|
|
|
|
, mFoundResponse(false)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
RunSyncWithDBOnTarget(const QuotaInfo& aQuotaInfo, nsIFile* aDBDir,
|
2015-03-21 19:28:04 +03:00
|
|
|
mozIStorageConnection* aConn) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-04-16 22:00:16 +03:00
|
|
|
nsresult rv = db::CacheMatch(aConn, mCacheId, mArgs.request(),
|
|
|
|
mArgs.params(), &mFoundResponse, &mResponse);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
2015-04-23 16:17:37 +03:00
|
|
|
if (!mFoundResponse || !mResponse.mHasBodyId
|
|
|
|
|| IsHeadRequest(mArgs.request(), mArgs.params())) {
|
|
|
|
mResponse.mHasBodyId = false;
|
2015-03-02 16:20:00 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2017-09-15 22:25:41 +03:00
|
|
|
if (mArgs.openMode() == OpenMode::Eager) {
|
|
|
|
rv = BodyOpen(aQuotaInfo, aDBDir, mResponse.mBodyId, getter_AddRefs(stream));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
if (NS_WARN_IF(!stream)) { return NS_ERROR_FILE_NOT_FOUND; }
|
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
mStreamList->Add(mResponse.mBodyId, std::move(stream));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
2015-04-16 22:00:15 +03:00
|
|
|
Complete(Listener* aListener, ErrorResult&& aRv) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
if (!mFoundResponse) {
|
2018-05-30 22:15:35 +03:00
|
|
|
aListener->OnOpComplete(std::move(aRv), CacheMatchResult(void_t()));
|
2015-03-02 16:20:00 +03:00
|
|
|
} else {
|
|
|
|
mStreamList->Activate(mCacheId);
|
2018-05-30 22:15:35 +03:00
|
|
|
aListener->OnOpComplete(std::move(aRv), CacheMatchResult(void_t()), mResponse,
|
2015-04-16 22:00:15 +03:00
|
|
|
mStreamList);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
mStreamList = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool MatchesCacheId(CacheId aCacheId) const override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
return aCacheId == mCacheId;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const CacheId mCacheId;
|
2015-04-16 22:00:15 +03:00
|
|
|
const CacheMatchArgs mArgs;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<StreamList> mStreamList;
|
2015-03-02 16:20:00 +03:00
|
|
|
bool mFoundResponse;
|
|
|
|
SavedResponse mResponse;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class Manager::CacheMatchAllAction final : public Manager::BaseAction
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CacheMatchAllAction(Manager* aManager, ListenerId aListenerId,
|
2015-04-16 22:00:15 +03:00
|
|
|
CacheId aCacheId, const CacheMatchAllArgs& aArgs,
|
2015-03-02 16:20:00 +03:00
|
|
|
StreamList* aStreamList)
|
2015-04-16 22:00:15 +03:00
|
|
|
: BaseAction(aManager, aListenerId)
|
2015-03-02 16:20:00 +03:00
|
|
|
, mCacheId(aCacheId)
|
2015-04-16 22:00:15 +03:00
|
|
|
, mArgs(aArgs)
|
2015-03-02 16:20:00 +03:00
|
|
|
, mStreamList(aStreamList)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
RunSyncWithDBOnTarget(const QuotaInfo& aQuotaInfo, nsIFile* aDBDir,
|
2015-03-21 19:28:04 +03:00
|
|
|
mozIStorageConnection* aConn) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-04-16 22:00:16 +03:00
|
|
|
nsresult rv = db::CacheMatchAll(aConn, mCacheId, mArgs.requestOrVoid(),
|
|
|
|
mArgs.params(), mSavedResponses);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mSavedResponses.Length(); ++i) {
|
2015-04-23 16:17:37 +03:00
|
|
|
if (!mSavedResponses[i].mHasBodyId
|
|
|
|
|| IsHeadRequest(mArgs.requestOrVoid(), mArgs.params())) {
|
|
|
|
mSavedResponses[i].mHasBodyId = false;
|
2015-03-02 16:20:00 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2017-09-15 22:25:41 +03:00
|
|
|
if (mArgs.openMode() == OpenMode::Eager) {
|
|
|
|
rv = BodyOpen(aQuotaInfo, aDBDir, mSavedResponses[i].mBodyId,
|
|
|
|
getter_AddRefs(stream));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
if (NS_WARN_IF(!stream)) { return NS_ERROR_FILE_NOT_FOUND; }
|
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
mStreamList->Add(mSavedResponses[i].mBodyId, std::move(stream));
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
2015-04-16 22:00:15 +03:00
|
|
|
Complete(Listener* aListener, ErrorResult&& aRv) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
mStreamList->Activate(mCacheId);
|
2018-05-30 22:15:35 +03:00
|
|
|
aListener->OnOpComplete(std::move(aRv), CacheMatchAllResult(), mSavedResponses,
|
2015-04-16 22:00:15 +03:00
|
|
|
mStreamList);
|
2015-03-02 16:20:00 +03:00
|
|
|
mStreamList = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool MatchesCacheId(CacheId aCacheId) const override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
return aCacheId == mCacheId;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const CacheId mCacheId;
|
2015-04-16 22:00:15 +03:00
|
|
|
const CacheMatchAllArgs mArgs;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<StreamList> mStreamList;
|
2015-03-02 16:20:00 +03:00
|
|
|
nsTArray<SavedResponse> mSavedResponses;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// This is the most complex Action. It puts a request/response pair into the
|
|
|
|
// Cache. It does not complete until all of the body data has been saved to
|
|
|
|
// disk. This means its an asynchronous Action.
|
2015-03-21 19:28:04 +03:00
|
|
|
class Manager::CachePutAllAction final : public DBAction
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CachePutAllAction(Manager* aManager, ListenerId aListenerId,
|
2015-04-16 22:00:15 +03:00
|
|
|
CacheId aCacheId,
|
2015-03-02 16:20:00 +03:00
|
|
|
const nsTArray<CacheRequestResponse>& aPutList,
|
|
|
|
const nsTArray<nsCOMPtr<nsIInputStream>>& aRequestStreamList,
|
|
|
|
const nsTArray<nsCOMPtr<nsIInputStream>>& aResponseStreamList)
|
|
|
|
: DBAction(DBAction::Existing)
|
|
|
|
, mManager(aManager)
|
|
|
|
, mListenerId(aListenerId)
|
|
|
|
, mCacheId(aCacheId)
|
|
|
|
, mList(aPutList.Length())
|
|
|
|
, mExpectedAsyncCopyCompletions(1)
|
|
|
|
, mAsyncResult(NS_OK)
|
|
|
|
, mMutex("cache::Manager::CachePutAllAction")
|
2017-07-10 12:02:44 +03:00
|
|
|
, mUpdatedPaddingSize(0)
|
2017-07-10 11:54:20 +03:00
|
|
|
, mDeletedPaddingSize(0)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!aPutList.IsEmpty());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aPutList.Length() == aRequestStreamList.Length());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aPutList.Length() == aResponseStreamList.Length());
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aPutList.Length(); ++i) {
|
|
|
|
Entry* entry = mList.AppendElement();
|
|
|
|
entry->mRequest = aPutList[i].request();
|
|
|
|
entry->mRequestStream = aRequestStreamList[i];
|
|
|
|
entry->mResponse = aPutList[i].response();
|
|
|
|
entry->mResponseStream = aResponseStreamList[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~CachePutAllAction() { }
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
RunWithDBOnTarget(Resolver* aResolver, const QuotaInfo& aQuotaInfo,
|
2015-03-21 19:28:04 +03:00
|
|
|
nsIFile* aDBDir, mozIStorageConnection* aConn) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aResolver);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aDBDir);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aConn);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mResolver);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mDBDir);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mConn);
|
|
|
|
|
2017-06-01 23:42:05 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mTarget);
|
|
|
|
mTarget = GetCurrentThreadSerialEventTarget();
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mTarget);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
// We should be pre-initialized to expect one async completion. This is
|
|
|
|
// the "manual" completion we call at the end of this method in all
|
|
|
|
// cases.
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mExpectedAsyncCopyCompletions == 1);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
mResolver = aResolver;
|
|
|
|
mDBDir = aDBDir;
|
|
|
|
mConn = aConn;
|
2017-05-23 13:33:47 +03:00
|
|
|
mQuotaInfo.emplace(aQuotaInfo);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
// File bodies are streamed to disk via asynchronous copying. Start
|
|
|
|
// this copying now. Each copy will eventually result in a call
|
|
|
|
// to OnAsyncCopyComplete().
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
for (uint32_t i = 0; i < mList.Length(); ++i) {
|
|
|
|
rv = StartStreamCopy(aQuotaInfo, mList[i], RequestStream,
|
|
|
|
&mExpectedAsyncCopyCompletions);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = StartStreamCopy(aQuotaInfo, mList[i], ResponseStream,
|
|
|
|
&mExpectedAsyncCopyCompletions);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always call OnAsyncCopyComplete() manually here. This covers the
|
|
|
|
// case where there is no async copying and also reports any startup
|
|
|
|
// errors correctly. If we hit an error, then OnAsyncCopyComplete()
|
|
|
|
// will cancel any async copying.
|
|
|
|
OnAsyncCopyComplete(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called once for each asynchronous file copy whether it succeeds or
|
|
|
|
// fails. If a file copy is canceled, it still calls this method with
|
|
|
|
// an error code.
|
|
|
|
void
|
|
|
|
OnAsyncCopyComplete(nsresult aRv)
|
|
|
|
{
|
2017-06-01 23:42:05 +03:00
|
|
|
MOZ_ASSERT(mTarget->IsOnCurrentThread());
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mConn);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mResolver);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mExpectedAsyncCopyCompletions > 0);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
// Explicitly check for cancellation here to catch a race condition.
|
|
|
|
// Consider:
|
|
|
|
//
|
|
|
|
// 1) NS_AsyncCopy() executes on IO thread, but has not saved its
|
|
|
|
// copy context yet.
|
|
|
|
// 2) CancelAllStreamCopying() occurs on PBackground thread
|
|
|
|
// 3) Copy context from (1) is saved on IO thread.
|
|
|
|
//
|
|
|
|
// Checking for cancellation here catches this condition when we
|
|
|
|
// first call OnAsyncCopyComplete() manually from RunWithDBOnTarget().
|
|
|
|
//
|
|
|
|
// This explicit cancellation check also handles the case where we
|
|
|
|
// are canceled just after all stream copying completes. We should
|
|
|
|
// abort the synchronous DB operations in this case if we have not
|
|
|
|
// started them yet.
|
|
|
|
if (NS_SUCCEEDED(aRv) && IsCanceled()) {
|
|
|
|
aRv = NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If any of the async copies fail, we need to still wait for them all to
|
|
|
|
// complete. Cancel any other streams still working and remember the
|
|
|
|
// error. All canceled streams will call OnAsyncCopyComplete().
|
|
|
|
if (NS_FAILED(aRv) && NS_SUCCEEDED(mAsyncResult)) {
|
|
|
|
CancelAllStreamCopying();
|
|
|
|
mAsyncResult = aRv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if async copying is still on-going. If so, then simply
|
|
|
|
// return for now. We must wait for a later OnAsyncCopyComplete() call.
|
|
|
|
mExpectedAsyncCopyCompletions -= 1;
|
|
|
|
if (mExpectedAsyncCopyCompletions > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have finished with all async copying. Indicate this by clearing all
|
|
|
|
// our copy contexts.
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mCopyContextList.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// An error occurred while async copying. Terminate the Action.
|
|
|
|
// DoResolve() will clean up any files we may have written.
|
|
|
|
if (NS_FAILED(mAsyncResult)) {
|
|
|
|
DoResolve(mAsyncResult);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mozStorageTransaction trans(mConn, false,
|
|
|
|
mozIStorageConnection::TRANSACTION_IMMEDIATE);
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
for (uint32_t i = 0; i < mList.Length(); ++i) {
|
|
|
|
Entry& e = mList[i];
|
|
|
|
if (e.mRequestStream) {
|
2015-04-16 22:00:16 +03:00
|
|
|
rv = BodyFinalizeWrite(mDBDir, e.mRequestBodyId);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
DoResolve(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (e.mResponseStream) {
|
2017-07-10 12:02:44 +03:00
|
|
|
// Gerenate padding size for opaque response if needed.
|
|
|
|
if (e.mResponse.type() == ResponseType::Opaque) {
|
|
|
|
// It'll generate padding if we've not set it yet.
|
|
|
|
rv = BodyMaybeUpdatePaddingSize(mQuotaInfo.ref(), mDBDir,
|
|
|
|
e.mResponseBodyId,
|
2017-07-10 12:03:24 +03:00
|
|
|
e.mResponse.paddingInfo(),
|
2017-07-10 12:02:44 +03:00
|
|
|
&e.mResponse.paddingSize());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
DoResolve(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(INT64_MAX - e.mResponse.paddingSize() >=
|
|
|
|
mUpdatedPaddingSize);
|
|
|
|
mUpdatedPaddingSize += e.mResponse.paddingSize();
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:16 +03:00
|
|
|
rv = BodyFinalizeWrite(mDBDir, e.mResponseBodyId);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
DoResolve(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-10 12:02:44 +03:00
|
|
|
int64_t deletedPaddingSize = 0;
|
2015-04-16 22:00:16 +03:00
|
|
|
rv = db::CachePut(mConn, mCacheId, e.mRequest,
|
|
|
|
e.mRequestStream ? &e.mRequestBodyId : nullptr,
|
|
|
|
e.mResponse,
|
|
|
|
e.mResponseStream ? &e.mResponseBodyId : nullptr,
|
2017-07-10 12:02:44 +03:00
|
|
|
mDeletedBodyIdList, &deletedPaddingSize);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
DoResolve(rv);
|
|
|
|
return;
|
|
|
|
}
|
2017-07-10 12:02:44 +03:00
|
|
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(INT64_MAX - mDeletedPaddingSize >=
|
|
|
|
deletedPaddingSize);
|
|
|
|
mDeletedPaddingSize += deletedPaddingSize;
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2017-07-18 14:00:09 +03:00
|
|
|
// Update padding file when it's necessary
|
|
|
|
rv = MaybeUpdatePaddingFile(mDBDir, mConn, mUpdatedPaddingSize,
|
|
|
|
mDeletedPaddingSize,
|
|
|
|
[&trans]() mutable { return trans.Commit(); });
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << NS_WARN_IF(NS_FAILED(rv));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
DoResolve(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
2015-03-21 19:28:04 +03:00
|
|
|
CompleteOnInitiatingThread(nsresult aRv) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Action);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mList.Length(); ++i) {
|
|
|
|
mList[i].mRequestStream = nullptr;
|
|
|
|
mList[i].mResponseStream = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-10-03 13:36:44 +03:00
|
|
|
// If the transaction fails, we shouldn't delete the body files and decrease
|
|
|
|
// their padding size.
|
|
|
|
if (NS_FAILED(aRv)) {
|
|
|
|
mDeletedBodyIdList.Clear();
|
|
|
|
mDeletedPaddingSize = 0;
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
mManager->NoteOrphanedBodyIdList(mDeletedBodyIdList);
|
|
|
|
|
2017-07-10 12:02:44 +03:00
|
|
|
if (mDeletedPaddingSize > 0) {
|
|
|
|
DecreaseUsageForQuotaInfo(mQuotaInfo.ref(), mDeletedPaddingSize);
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
Listener* listener = mManager->GetListener(mListenerId);
|
|
|
|
mManager = nullptr;
|
|
|
|
if (listener) {
|
2015-04-16 22:00:15 +03:00
|
|
|
listener->OnOpComplete(ErrorResult(aRv), CachePutAllResult());
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
2015-03-21 19:28:04 +03:00
|
|
|
CancelOnInitiatingThread() override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Action);
|
|
|
|
Action::CancelOnInitiatingThread();
|
|
|
|
CancelAllStreamCopying();
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool MatchesCacheId(CacheId aCacheId) const override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Action);
|
|
|
|
return aCacheId == mCacheId;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Entry
|
|
|
|
{
|
2015-04-16 22:00:15 +03:00
|
|
|
CacheRequest mRequest;
|
2015-03-02 16:20:00 +03:00
|
|
|
nsCOMPtr<nsIInputStream> mRequestStream;
|
|
|
|
nsID mRequestBodyId;
|
|
|
|
nsCOMPtr<nsISupports> mRequestCopyContext;
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
CacheResponse mResponse;
|
2015-03-02 16:20:00 +03:00
|
|
|
nsCOMPtr<nsIInputStream> mResponseStream;
|
|
|
|
nsID mResponseBodyId;
|
|
|
|
nsCOMPtr<nsISupports> mResponseCopyContext;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum StreamId
|
|
|
|
{
|
|
|
|
RequestStream,
|
|
|
|
ResponseStream
|
|
|
|
};
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
StartStreamCopy(const QuotaInfo& aQuotaInfo, Entry& aEntry,
|
|
|
|
StreamId aStreamId, uint32_t* aCopyCountOut)
|
|
|
|
{
|
2017-06-01 23:42:05 +03:00
|
|
|
MOZ_ASSERT(mTarget->IsOnCurrentThread());
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aCopyCountOut);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
if (IsCanceled()) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> source;
|
|
|
|
nsID* bodyId;
|
|
|
|
|
|
|
|
if (aStreamId == RequestStream) {
|
|
|
|
source = aEntry.mRequestStream;
|
|
|
|
bodyId = &aEntry.mRequestBodyId;
|
|
|
|
} else {
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aStreamId == ResponseStream);
|
2015-03-02 16:20:00 +03:00
|
|
|
source = aEntry.mResponseStream;
|
|
|
|
bodyId = &aEntry.mResponseBodyId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!source) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> copyContext;
|
|
|
|
|
2015-04-16 22:00:16 +03:00
|
|
|
nsresult rv = BodyStartWriteStream(aQuotaInfo, mDBDir, source, this,
|
|
|
|
AsyncCopyCompleteFunc, bodyId,
|
|
|
|
getter_AddRefs(copyContext));
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
|
|
|
mBodyIdWrittenList.AppendElement(*bodyId);
|
|
|
|
|
|
|
|
if (copyContext) {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mCopyContextList.AppendElement(copyContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
*aCopyCountOut += 1;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CancelAllStreamCopying()
|
|
|
|
{
|
|
|
|
// May occur on either owning thread or target thread
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
for (uint32_t i = 0; i < mCopyContextList.Length(); ++i) {
|
2015-04-16 22:00:16 +03:00
|
|
|
BodyCancelWrite(mDBDir, mCopyContextList[i]);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
mCopyContextList.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
AsyncCopyCompleteFunc(void* aClosure, nsresult aRv)
|
|
|
|
{
|
|
|
|
// May be on any thread, including STS event target.
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aClosure);
|
2015-03-24 05:28:09 +03:00
|
|
|
// Weak ref as we are guaranteed to the action is alive until
|
|
|
|
// CompleteOnInitiatingThread is called.
|
|
|
|
CachePutAllAction* action = static_cast<CachePutAllAction*>(aClosure);
|
2015-03-02 16:20:00 +03:00
|
|
|
action->CallOnAsyncCopyCompleteOnTargetThread(aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CallOnAsyncCopyCompleteOnTargetThread(nsresult aRv)
|
|
|
|
{
|
2015-03-24 05:28:09 +03:00
|
|
|
// May be on any thread, including STS event target. Non-owning runnable
|
|
|
|
// here since we are guaranteed the Action will survive until
|
|
|
|
// CompleteOnInitiatingThread is called.
|
2016-05-05 11:45:00 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable = NewNonOwningRunnableMethod<nsresult>(
|
2017-06-12 22:34:10 +03:00
|
|
|
"dom::cache::Manager::CachePutAllAction::OnAsyncCopyComplete",
|
|
|
|
this,
|
|
|
|
&CachePutAllAction::OnAsyncCopyComplete,
|
|
|
|
aRv);
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
2017-06-01 23:42:05 +03:00
|
|
|
mTarget->Dispatch(runnable.forget(), nsIThread::DISPATCH_NORMAL));
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DoResolve(nsresult aRv)
|
|
|
|
{
|
2017-06-01 23:42:05 +03:00
|
|
|
MOZ_ASSERT(mTarget->IsOnCurrentThread());
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
// DoResolve() must not be called until all async copying has completed.
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
MOZ_ASSERT(mCopyContextList.IsEmpty());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Clean up any files we might have written before hitting the error.
|
|
|
|
if (NS_FAILED(aRv)) {
|
2017-05-23 13:33:47 +03:00
|
|
|
BodyDeleteFiles(mQuotaInfo.ref(), mDBDir, mBodyIdWrittenList);
|
2017-07-10 12:02:44 +03:00
|
|
|
if (mUpdatedPaddingSize > 0) {
|
|
|
|
DecreaseUsageForQuotaInfo(mQuotaInfo.ref(), mUpdatedPaddingSize);
|
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Must be released on the target thread where it was opened.
|
|
|
|
mConn = nullptr;
|
|
|
|
|
|
|
|
// Drop our ref to the target thread as we are done with this thread.
|
|
|
|
// Also makes our thread assertions catch any incorrect method calls
|
|
|
|
// after resolve.
|
2017-06-01 23:42:05 +03:00
|
|
|
mTarget = nullptr;
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
// Make sure to de-ref the resolver per the Action API contract.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Action::Resolver> resolver;
|
2015-03-02 16:20:00 +03:00
|
|
|
mResolver.swap(resolver);
|
|
|
|
resolver->Resolve(aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// initiating thread only
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Manager> mManager;
|
2015-03-02 16:20:00 +03:00
|
|
|
const ListenerId mListenerId;
|
|
|
|
|
|
|
|
// Set on initiating thread, read on target thread. State machine guarantees
|
|
|
|
// these are not modified while being read by the target thread.
|
|
|
|
const CacheId mCacheId;
|
|
|
|
nsTArray<Entry> mList;
|
|
|
|
uint32_t mExpectedAsyncCopyCompletions;
|
|
|
|
|
|
|
|
// target thread only
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Resolver> mResolver;
|
2015-03-02 16:20:00 +03:00
|
|
|
nsCOMPtr<nsIFile> mDBDir;
|
|
|
|
nsCOMPtr<mozIStorageConnection> mConn;
|
2017-06-01 23:42:05 +03:00
|
|
|
nsCOMPtr<nsISerialEventTarget> mTarget;
|
2015-03-02 16:20:00 +03:00
|
|
|
nsresult mAsyncResult;
|
|
|
|
nsTArray<nsID> mBodyIdWrittenList;
|
|
|
|
|
|
|
|
// Written to on target thread, accessed on initiating thread after target
|
|
|
|
// thread activity is guaranteed complete
|
|
|
|
nsTArray<nsID> mDeletedBodyIdList;
|
|
|
|
|
|
|
|
// accessed from any thread while mMutex locked
|
|
|
|
Mutex mMutex;
|
|
|
|
nsTArray<nsCOMPtr<nsISupports>> mCopyContextList;
|
2017-05-23 13:33:47 +03:00
|
|
|
|
|
|
|
Maybe<QuotaInfo> mQuotaInfo;
|
2017-07-10 12:02:44 +03:00
|
|
|
// Track how much pad amount has been added for new entries so that it can be
|
|
|
|
// removed if an error occurs.
|
|
|
|
int64_t mUpdatedPaddingSize;
|
2017-07-10 11:54:20 +03:00
|
|
|
// Track any pad amount associated with overwritten entries.
|
|
|
|
int64_t mDeletedPaddingSize;
|
2015-03-02 16:20:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class Manager::CacheDeleteAction final : public Manager::BaseAction
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CacheDeleteAction(Manager* aManager, ListenerId aListenerId,
|
2015-04-16 22:00:15 +03:00
|
|
|
CacheId aCacheId, const CacheDeleteArgs& aArgs)
|
|
|
|
: BaseAction(aManager, aListenerId)
|
2015-03-02 16:20:00 +03:00
|
|
|
, mCacheId(aCacheId)
|
2015-04-16 22:00:15 +03:00
|
|
|
, mArgs(aArgs)
|
2015-03-02 16:20:00 +03:00
|
|
|
, mSuccess(false)
|
2017-07-10 11:54:20 +03:00
|
|
|
, mDeletedPaddingSize(0)
|
2015-03-02 16:20:00 +03:00
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
RunSyncWithDBOnTarget(const QuotaInfo& aQuotaInfo, nsIFile* aDBDir,
|
2015-03-21 19:28:04 +03:00
|
|
|
mozIStorageConnection* aConn) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2017-07-10 12:02:44 +03:00
|
|
|
mQuotaInfo.emplace(aQuotaInfo);
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
mozStorageTransaction trans(aConn, false,
|
|
|
|
mozIStorageConnection::TRANSACTION_IMMEDIATE);
|
|
|
|
|
2015-04-16 22:00:16 +03:00
|
|
|
nsresult rv = db::CacheDelete(aConn, mCacheId, mArgs.request(),
|
|
|
|
mArgs.params(), mDeletedBodyIdList,
|
2017-07-10 11:54:20 +03:00
|
|
|
&mDeletedPaddingSize, &mSuccess);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
2017-07-18 14:00:09 +03:00
|
|
|
rv = MaybeUpdatePaddingFile(aDBDir, aConn, /* aIncreaceSize */ 0,
|
|
|
|
mDeletedPaddingSize,
|
|
|
|
[&trans]() mutable { return trans.Commit(); });
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mSuccess = false;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
2015-04-16 22:00:15 +03:00
|
|
|
Complete(Listener* aListener, ErrorResult&& aRv) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2017-10-03 13:36:44 +03:00
|
|
|
// If the transaction fails, we shouldn't delete the body files and decrease
|
|
|
|
// their padding size.
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
mDeletedBodyIdList.Clear();
|
|
|
|
mDeletedPaddingSize = 0;
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
mManager->NoteOrphanedBodyIdList(mDeletedBodyIdList);
|
2017-07-18 14:00:09 +03:00
|
|
|
|
|
|
|
if (mDeletedPaddingSize > 0) {
|
|
|
|
DecreaseUsageForQuotaInfo(mQuotaInfo.ref(), mDeletedPaddingSize);
|
|
|
|
}
|
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
aListener->OnOpComplete(std::move(aRv), CacheDeleteResult(mSuccess));
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool MatchesCacheId(CacheId aCacheId) const override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
return aCacheId == mCacheId;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const CacheId mCacheId;
|
2015-04-16 22:00:15 +03:00
|
|
|
const CacheDeleteArgs mArgs;
|
2015-03-02 16:20:00 +03:00
|
|
|
bool mSuccess;
|
|
|
|
nsTArray<nsID> mDeletedBodyIdList;
|
2017-07-10 12:02:44 +03:00
|
|
|
Maybe<QuotaInfo> mQuotaInfo;
|
2017-07-10 11:54:20 +03:00
|
|
|
// Track any pad amount associated with deleted entries.
|
|
|
|
int64_t mDeletedPaddingSize;
|
2015-03-02 16:20:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class Manager::CacheKeysAction final : public Manager::BaseAction
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CacheKeysAction(Manager* aManager, ListenerId aListenerId,
|
2015-04-16 22:00:15 +03:00
|
|
|
CacheId aCacheId, const CacheKeysArgs& aArgs,
|
2015-03-02 16:20:00 +03:00
|
|
|
StreamList* aStreamList)
|
2015-04-16 22:00:15 +03:00
|
|
|
: BaseAction(aManager, aListenerId)
|
2015-03-02 16:20:00 +03:00
|
|
|
, mCacheId(aCacheId)
|
2015-04-16 22:00:15 +03:00
|
|
|
, mArgs(aArgs)
|
2015-03-02 16:20:00 +03:00
|
|
|
, mStreamList(aStreamList)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
RunSyncWithDBOnTarget(const QuotaInfo& aQuotaInfo, nsIFile* aDBDir,
|
2015-03-21 19:28:04 +03:00
|
|
|
mozIStorageConnection* aConn) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-04-16 22:00:16 +03:00
|
|
|
nsresult rv = db::CacheKeys(aConn, mCacheId, mArgs.requestOrVoid(),
|
|
|
|
mArgs.params(), mSavedRequests);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mSavedRequests.Length(); ++i) {
|
2015-04-23 16:17:37 +03:00
|
|
|
if (!mSavedRequests[i].mHasBodyId
|
|
|
|
|| IsHeadRequest(mArgs.requestOrVoid(), mArgs.params())) {
|
|
|
|
mSavedRequests[i].mHasBodyId = false;
|
2015-03-02 16:20:00 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2017-09-15 22:25:41 +03:00
|
|
|
if (mArgs.openMode() == OpenMode::Eager) {
|
|
|
|
rv = BodyOpen(aQuotaInfo, aDBDir, mSavedRequests[i].mBodyId,
|
|
|
|
getter_AddRefs(stream));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
if (NS_WARN_IF(!stream)) { return NS_ERROR_FILE_NOT_FOUND; }
|
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
mStreamList->Add(mSavedRequests[i].mBodyId, std::move(stream));
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
2015-04-16 22:00:15 +03:00
|
|
|
Complete(Listener* aListener, ErrorResult&& aRv) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
mStreamList->Activate(mCacheId);
|
2018-05-30 22:15:35 +03:00
|
|
|
aListener->OnOpComplete(std::move(aRv), CacheKeysResult(), mSavedRequests,
|
2015-04-16 22:00:15 +03:00
|
|
|
mStreamList);
|
2015-03-02 16:20:00 +03:00
|
|
|
mStreamList = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool MatchesCacheId(CacheId aCacheId) const override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
return aCacheId == mCacheId;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const CacheId mCacheId;
|
2015-04-16 22:00:15 +03:00
|
|
|
const CacheKeysArgs mArgs;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<StreamList> mStreamList;
|
2015-03-02 16:20:00 +03:00
|
|
|
nsTArray<SavedRequest> mSavedRequests;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class Manager::StorageMatchAction final : public Manager::BaseAction
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
StorageMatchAction(Manager* aManager, ListenerId aListenerId,
|
2015-04-16 22:00:15 +03:00
|
|
|
Namespace aNamespace,
|
|
|
|
const StorageMatchArgs& aArgs,
|
2015-03-02 16:20:00 +03:00
|
|
|
StreamList* aStreamList)
|
2015-04-16 22:00:15 +03:00
|
|
|
: BaseAction(aManager, aListenerId)
|
2015-03-02 16:20:00 +03:00
|
|
|
, mNamespace(aNamespace)
|
2015-04-16 22:00:15 +03:00
|
|
|
, mArgs(aArgs)
|
2015-03-02 16:20:00 +03:00
|
|
|
, mStreamList(aStreamList)
|
|
|
|
, mFoundResponse(false)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
RunSyncWithDBOnTarget(const QuotaInfo& aQuotaInfo, nsIFile* aDBDir,
|
2015-03-21 19:28:04 +03:00
|
|
|
mozIStorageConnection* aConn) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-04-16 22:00:16 +03:00
|
|
|
nsresult rv = db::StorageMatch(aConn, mNamespace, mArgs.request(),
|
|
|
|
mArgs.params(), &mFoundResponse,
|
|
|
|
&mSavedResponse);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
2015-04-23 16:17:37 +03:00
|
|
|
if (!mFoundResponse || !mSavedResponse.mHasBodyId
|
|
|
|
|| IsHeadRequest(mArgs.request(), mArgs.params())) {
|
|
|
|
mSavedResponse.mHasBodyId = false;
|
2015-03-02 16:20:00 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2017-09-15 22:25:41 +03:00
|
|
|
if (mArgs.openMode() == OpenMode::Eager) {
|
|
|
|
rv = BodyOpen(aQuotaInfo, aDBDir, mSavedResponse.mBodyId,
|
|
|
|
getter_AddRefs(stream));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
if (NS_WARN_IF(!stream)) { return NS_ERROR_FILE_NOT_FOUND; }
|
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
mStreamList->Add(mSavedResponse.mBodyId, std::move(stream));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
2015-04-16 22:00:15 +03:00
|
|
|
Complete(Listener* aListener, ErrorResult&& aRv) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
if (!mFoundResponse) {
|
2018-05-30 22:15:35 +03:00
|
|
|
aListener->OnOpComplete(std::move(aRv), StorageMatchResult(void_t()));
|
2015-03-02 16:20:00 +03:00
|
|
|
} else {
|
|
|
|
mStreamList->Activate(mSavedResponse.mCacheId);
|
2018-05-30 22:15:35 +03:00
|
|
|
aListener->OnOpComplete(std::move(aRv), StorageMatchResult(void_t()), mSavedResponse,
|
2015-04-16 22:00:15 +03:00
|
|
|
mStreamList);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
mStreamList = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Namespace mNamespace;
|
2015-04-16 22:00:15 +03:00
|
|
|
const StorageMatchArgs mArgs;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<StreamList> mStreamList;
|
2015-03-02 16:20:00 +03:00
|
|
|
bool mFoundResponse;
|
|
|
|
SavedResponse mSavedResponse;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class Manager::StorageHasAction final : public Manager::BaseAction
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
StorageHasAction(Manager* aManager, ListenerId aListenerId,
|
2015-04-16 22:00:15 +03:00
|
|
|
Namespace aNamespace, const StorageHasArgs& aArgs)
|
|
|
|
: BaseAction(aManager, aListenerId)
|
2015-03-02 16:20:00 +03:00
|
|
|
, mNamespace(aNamespace)
|
2015-04-16 22:00:15 +03:00
|
|
|
, mArgs(aArgs)
|
2015-03-02 16:20:00 +03:00
|
|
|
, mCacheFound(false)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
RunSyncWithDBOnTarget(const QuotaInfo& aQuotaInfo, nsIFile* aDBDir,
|
2015-03-21 19:28:04 +03:00
|
|
|
mozIStorageConnection* aConn) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
CacheId cacheId;
|
2015-04-16 22:00:16 +03:00
|
|
|
return db::StorageGetCacheId(aConn, mNamespace, mArgs.key(),
|
|
|
|
&mCacheFound, &cacheId);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
2015-04-16 22:00:15 +03:00
|
|
|
Complete(Listener* aListener, ErrorResult&& aRv) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2018-05-30 22:15:35 +03:00
|
|
|
aListener->OnOpComplete(std::move(aRv), StorageHasResult(mCacheFound));
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Namespace mNamespace;
|
2015-04-16 22:00:15 +03:00
|
|
|
const StorageHasArgs mArgs;
|
2015-03-02 16:20:00 +03:00
|
|
|
bool mCacheFound;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class Manager::StorageOpenAction final : public Manager::BaseAction
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
StorageOpenAction(Manager* aManager, ListenerId aListenerId,
|
2015-04-16 22:00:15 +03:00
|
|
|
Namespace aNamespace, const StorageOpenArgs& aArgs)
|
|
|
|
: BaseAction(aManager, aListenerId)
|
2015-03-02 16:20:00 +03:00
|
|
|
, mNamespace(aNamespace)
|
2015-04-16 22:00:15 +03:00
|
|
|
, mArgs(aArgs)
|
2015-04-03 03:11:41 +03:00
|
|
|
, mCacheId(INVALID_CACHE_ID)
|
2015-03-02 16:20:00 +03:00
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
RunSyncWithDBOnTarget(const QuotaInfo& aQuotaInfo, nsIFile* aDBDir,
|
2015-03-21 19:28:04 +03:00
|
|
|
mozIStorageConnection* aConn) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
// Cache does not exist, create it instead
|
|
|
|
mozStorageTransaction trans(aConn, false,
|
|
|
|
mozIStorageConnection::TRANSACTION_IMMEDIATE);
|
|
|
|
|
|
|
|
// Look for existing cache
|
|
|
|
bool cacheFound;
|
2015-04-16 22:00:16 +03:00
|
|
|
nsresult rv = db::StorageGetCacheId(aConn, mNamespace, mArgs.key(),
|
|
|
|
&cacheFound, &mCacheId);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
if (cacheFound) {
|
2017-01-10 19:20:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mCacheId != INVALID_CACHE_ID);
|
2015-03-02 16:20:00 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:16 +03:00
|
|
|
rv = db::CreateCacheId(aConn, &mCacheId);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
2015-04-16 22:00:16 +03:00
|
|
|
rv = db::StoragePutCache(aConn, mNamespace, mArgs.key(), mCacheId);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
|
|
|
rv = trans.Commit();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
2017-01-10 19:20:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mCacheId != INVALID_CACHE_ID);
|
2015-03-02 16:20:00 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
2015-04-16 22:00:15 +03:00
|
|
|
Complete(Listener* aListener, ErrorResult&& aRv) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2017-01-10 19:20:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aRv.Failed() || mCacheId != INVALID_CACHE_ID);
|
2018-05-30 22:15:35 +03:00
|
|
|
aListener->OnOpComplete(std::move(aRv),
|
2017-09-15 22:25:41 +03:00
|
|
|
StorageOpenResult(nullptr, nullptr, mNamespace),
|
|
|
|
mCacheId);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Namespace mNamespace;
|
2015-04-16 22:00:15 +03:00
|
|
|
const StorageOpenArgs mArgs;
|
2015-03-02 16:20:00 +03:00
|
|
|
CacheId mCacheId;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class Manager::StorageDeleteAction final : public Manager::BaseAction
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
StorageDeleteAction(Manager* aManager, ListenerId aListenerId,
|
2015-04-16 22:00:15 +03:00
|
|
|
Namespace aNamespace, const StorageDeleteArgs& aArgs)
|
|
|
|
: BaseAction(aManager, aListenerId)
|
2015-03-02 16:20:00 +03:00
|
|
|
, mNamespace(aNamespace)
|
2015-04-16 22:00:15 +03:00
|
|
|
, mArgs(aArgs)
|
2015-03-02 16:20:00 +03:00
|
|
|
, mCacheDeleted(false)
|
2015-04-03 03:11:41 +03:00
|
|
|
, mCacheId(INVALID_CACHE_ID)
|
2015-03-02 16:20:00 +03:00
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
RunSyncWithDBOnTarget(const QuotaInfo& aQuotaInfo, nsIFile* aDBDir,
|
2015-03-21 19:28:04 +03:00
|
|
|
mozIStorageConnection* aConn) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
mozStorageTransaction trans(aConn, false,
|
|
|
|
mozIStorageConnection::TRANSACTION_IMMEDIATE);
|
|
|
|
|
|
|
|
bool exists;
|
2015-04-16 22:00:16 +03:00
|
|
|
nsresult rv = db::StorageGetCacheId(aConn, mNamespace, mArgs.key(),
|
|
|
|
&exists, &mCacheId);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
|
|
|
if (!exists) {
|
|
|
|
mCacheDeleted = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-10 11:54:20 +03:00
|
|
|
// Don't delete the removing padding size here, we'll delete it on
|
|
|
|
// DeleteOrphanedCacheAction.
|
2015-04-16 22:00:16 +03:00
|
|
|
rv = db::StorageForgetCache(aConn, mNamespace, mArgs.key());
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
|
|
|
rv = trans.Commit();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
|
|
|
|
mCacheDeleted = true;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
2015-04-16 22:00:15 +03:00
|
|
|
Complete(Listener* aListener, ErrorResult&& aRv) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
if (mCacheDeleted) {
|
|
|
|
// If content is referencing this cache, mark it orphaned to be
|
|
|
|
// deleted later.
|
|
|
|
if (!mManager->SetCacheIdOrphanedIfRefed(mCacheId)) {
|
|
|
|
|
|
|
|
// no outstanding references, delete immediately
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Context> context = mManager->mContext;
|
2015-04-16 23:05:38 +03:00
|
|
|
|
2015-06-26 08:22:46 +03:00
|
|
|
if (context->IsCanceled()) {
|
|
|
|
context->NoteOrphanedData();
|
|
|
|
} else {
|
2015-04-16 23:05:38 +03:00
|
|
|
context->CancelForCacheId(mCacheId);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Action> action =
|
2015-04-16 23:05:38 +03:00
|
|
|
new DeleteOrphanedCacheAction(mManager, mCacheId);
|
2015-05-07 15:16:51 +03:00
|
|
|
context->Dispatch(action);
|
2015-04-16 23:05:38 +03:00
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
aListener->OnOpComplete(std::move(aRv), StorageDeleteResult(mCacheDeleted));
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Namespace mNamespace;
|
2015-04-16 22:00:15 +03:00
|
|
|
const StorageDeleteArgs mArgs;
|
2015-03-02 16:20:00 +03:00
|
|
|
bool mCacheDeleted;
|
|
|
|
CacheId mCacheId;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class Manager::StorageKeysAction final : public Manager::BaseAction
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
StorageKeysAction(Manager* aManager, ListenerId aListenerId,
|
2015-04-16 22:00:15 +03:00
|
|
|
Namespace aNamespace)
|
|
|
|
: BaseAction(aManager, aListenerId)
|
2015-03-02 16:20:00 +03:00
|
|
|
, mNamespace(aNamespace)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
RunSyncWithDBOnTarget(const QuotaInfo& aQuotaInfo, nsIFile* aDBDir,
|
2015-03-21 19:28:04 +03:00
|
|
|
mozIStorageConnection* aConn) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-04-16 22:00:16 +03:00
|
|
|
return db::StorageGetKeys(aConn, mNamespace, mKeys);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
2015-04-16 22:00:15 +03:00
|
|
|
Complete(Listener* aListener, ErrorResult&& aRv) override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-04-16 22:00:15 +03:00
|
|
|
if (aRv.Failed()) {
|
2015-03-02 16:20:00 +03:00
|
|
|
mKeys.Clear();
|
|
|
|
}
|
2018-05-30 22:15:35 +03:00
|
|
|
aListener->OnOpComplete(std::move(aRv), StorageKeysResult(mKeys));
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Namespace mNamespace;
|
|
|
|
nsTArray<nsString> mKeys;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2017-09-15 22:25:40 +03:00
|
|
|
class Manager::OpenStreamAction final : public Manager::BaseAction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
OpenStreamAction(Manager* aManager, ListenerId aListenerId,
|
2017-09-15 22:25:41 +03:00
|
|
|
InputStreamResolver&& aResolver, const nsID& aBodyId)
|
2017-09-15 22:25:40 +03:00
|
|
|
: BaseAction(aManager, aListenerId)
|
2018-05-30 22:15:35 +03:00
|
|
|
, mResolver(std::move(aResolver))
|
2017-09-15 22:25:40 +03:00
|
|
|
, mBodyId(aBodyId)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual nsresult
|
|
|
|
RunSyncWithDBOnTarget(const QuotaInfo& aQuotaInfo, nsIFile* aDBDir,
|
|
|
|
mozIStorageConnection* aConn) override
|
|
|
|
{
|
|
|
|
nsresult rv = BodyOpen(aQuotaInfo, aDBDir, mBodyId,
|
|
|
|
getter_AddRefs(mBodyStream));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
|
|
|
|
if (NS_WARN_IF(!mBodyStream)) { return NS_ERROR_FILE_NOT_FOUND; }
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
Complete(Listener* aListener, ErrorResult&& aRv) override
|
|
|
|
{
|
2018-05-30 22:15:35 +03:00
|
|
|
mResolver(std::move(mBodyStream));
|
2017-09-15 22:25:40 +03:00
|
|
|
mResolver = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-09-15 22:25:41 +03:00
|
|
|
InputStreamResolver mResolver;
|
2017-09-15 22:25:40 +03:00
|
|
|
const nsID mBodyId;
|
|
|
|
nsCOMPtr<nsIInputStream> mBodyStream;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
//static
|
|
|
|
Manager::ListenerId Manager::sNextListenerId = 0;
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
void
|
|
|
|
Manager::Listener::OnOpComplete(ErrorResult&& aRv, const CacheOpResult& aResult)
|
|
|
|
{
|
2018-05-30 22:15:35 +03:00
|
|
|
OnOpComplete(std::move(aRv), aResult, INVALID_CACHE_ID, nsTArray<SavedResponse>(),
|
2015-04-16 22:00:15 +03:00
|
|
|
nsTArray<SavedRequest>(), nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Manager::Listener::OnOpComplete(ErrorResult&& aRv, const CacheOpResult& aResult,
|
|
|
|
CacheId aOpenedCacheId)
|
|
|
|
{
|
2018-05-30 22:15:35 +03:00
|
|
|
OnOpComplete(std::move(aRv), aResult, aOpenedCacheId, nsTArray<SavedResponse>(),
|
2015-04-16 22:00:15 +03:00
|
|
|
nsTArray<SavedRequest>(), nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Manager::Listener::OnOpComplete(ErrorResult&& aRv, const CacheOpResult& aResult,
|
|
|
|
const SavedResponse& aSavedResponse,
|
|
|
|
StreamList* aStreamList)
|
|
|
|
{
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<SavedResponse, 1> responseList;
|
2015-04-16 22:00:15 +03:00
|
|
|
responseList.AppendElement(aSavedResponse);
|
2018-05-30 22:15:35 +03:00
|
|
|
OnOpComplete(std::move(aRv), aResult, INVALID_CACHE_ID, responseList,
|
2015-04-16 22:00:15 +03:00
|
|
|
nsTArray<SavedRequest>(), aStreamList);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Manager::Listener::OnOpComplete(ErrorResult&& aRv, const CacheOpResult& aResult,
|
|
|
|
const nsTArray<SavedResponse>& aSavedResponseList,
|
|
|
|
StreamList* aStreamList)
|
|
|
|
{
|
2018-05-30 22:15:35 +03:00
|
|
|
OnOpComplete(std::move(aRv), aResult, INVALID_CACHE_ID, aSavedResponseList,
|
2015-04-16 22:00:15 +03:00
|
|
|
nsTArray<SavedRequest>(), aStreamList);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Manager::Listener::OnOpComplete(ErrorResult&& aRv, const CacheOpResult& aResult,
|
|
|
|
const nsTArray<SavedRequest>& aSavedRequestList,
|
|
|
|
StreamList* aStreamList)
|
|
|
|
{
|
2018-05-30 22:15:35 +03:00
|
|
|
OnOpComplete(std::move(aRv), aResult, INVALID_CACHE_ID, nsTArray<SavedResponse>(),
|
2015-04-16 22:00:15 +03:00
|
|
|
aSavedRequestList, aStreamList);
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
Manager::GetOrCreate(ManagerId* aManagerId, Manager** aManagerOut)
|
|
|
|
{
|
|
|
|
mozilla::ipc::AssertIsOnBackgroundThread();
|
|
|
|
return Factory::GetOrCreate(aManagerId, aManagerOut);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<Manager>
|
|
|
|
Manager::Get(ManagerId* aManagerId)
|
|
|
|
{
|
|
|
|
mozilla::ipc::AssertIsOnBackgroundThread();
|
|
|
|
return Factory::Get(aManagerId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
2015-11-22 12:44:16 +03:00
|
|
|
Manager::ShutdownAll()
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-11-22 12:44:16 +03:00
|
|
|
mozilla::ipc::AssertIsOnBackgroundThread();
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-11-22 12:44:16 +03:00
|
|
|
Factory::ShutdownAll();
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2017-05-15 16:34:19 +03:00
|
|
|
if (!mozilla::SpinEventLoopUntil([]() {
|
|
|
|
return Factory::IsShutdownAllComplete();
|
|
|
|
})) {
|
|
|
|
NS_WARNING("Something bad happened!");
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-30 15:59:27 +03:00
|
|
|
// static
|
|
|
|
void
|
2015-11-22 12:44:16 +03:00
|
|
|
Manager::Abort(const nsACString& aOrigin)
|
2015-06-30 15:59:27 +03:00
|
|
|
{
|
2015-11-22 12:44:16 +03:00
|
|
|
mozilla::ipc::AssertIsOnBackgroundThread();
|
2015-06-30 15:59:27 +03:00
|
|
|
|
2015-11-22 12:44:16 +03:00
|
|
|
Factory::Abort(aOrigin);
|
2015-06-30 15:59:27 +03:00
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
void
|
|
|
|
Manager::RemoveListener(Listener* aListener)
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
|
|
|
// There may not be a listener here in the case where an actor is killed
|
|
|
|
// before it can perform any actual async requests on Manager.
|
|
|
|
mListeners.RemoveElement(aListener, ListenerEntryListenerComparator());
|
|
|
|
MOZ_ASSERT(!mListeners.Contains(aListener,
|
|
|
|
ListenerEntryListenerComparator()));
|
2015-04-03 03:39:46 +03:00
|
|
|
MaybeAllowContextToClose();
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Manager::RemoveContext(Context* aContext)
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mContext);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mContext == aContext);
|
2015-04-03 03:39:46 +03:00
|
|
|
|
|
|
|
// Whether the Context destruction was triggered from the Manager going
|
2015-04-16 23:05:38 +03:00
|
|
|
// idle or the underlying storage being invalidated, we should know we
|
2015-06-26 08:22:46 +03:00
|
|
|
// are closing before the Context is destroyed.
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState == Closing);
|
2015-04-03 03:39:46 +03:00
|
|
|
|
2015-06-26 08:22:46 +03:00
|
|
|
// Before forgetting the Context, check to see if we have any outstanding
|
|
|
|
// cache or body objects waiting for deletion. If so, note that we've
|
|
|
|
// orphaned data so it will be cleaned up on the next open.
|
|
|
|
for (uint32_t i = 0; i < mCacheIdRefs.Length(); ++i) {
|
|
|
|
if (mCacheIdRefs[i].mOrphaned) {
|
|
|
|
aContext->NoteOrphanedData();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mBodyIdRefs.Length(); ++i) {
|
|
|
|
if (mBodyIdRefs[i].mOrphaned) {
|
|
|
|
aContext->NoteOrphanedData();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
mContext = nullptr;
|
|
|
|
|
2015-04-16 23:05:38 +03:00
|
|
|
// Once the context is gone, we can immediately remove ourself from the
|
|
|
|
// Factory list. We don't need to block shutdown by staying in the list
|
|
|
|
// any more.
|
|
|
|
Factory::Remove(this);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2015-03-16 17:10:36 +03:00
|
|
|
void
|
2015-04-16 23:05:38 +03:00
|
|
|
Manager::NoteClosing()
|
2015-03-16 17:10:36 +03:00
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
2015-04-16 23:05:38 +03:00
|
|
|
// This can be called more than once legitimately through different paths.
|
2015-04-20 21:14:57 +03:00
|
|
|
mState = Closing;
|
2015-03-16 17:10:36 +03:00
|
|
|
}
|
|
|
|
|
2015-04-20 21:14:57 +03:00
|
|
|
Manager::State
|
|
|
|
Manager::GetState() const
|
2015-03-16 17:10:36 +03:00
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
2015-04-20 21:14:57 +03:00
|
|
|
return mState;
|
2015-03-16 17:10:36 +03:00
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
void
|
|
|
|
Manager::AddRefCacheId(CacheId aCacheId)
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
|
|
|
for (uint32_t i = 0; i < mCacheIdRefs.Length(); ++i) {
|
|
|
|
if (mCacheIdRefs[i].mCacheId == aCacheId) {
|
|
|
|
mCacheIdRefs[i].mCount += 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CacheIdRefCounter* entry = mCacheIdRefs.AppendElement();
|
|
|
|
entry->mCacheId = aCacheId;
|
|
|
|
entry->mCount = 1;
|
|
|
|
entry->mOrphaned = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Manager::ReleaseCacheId(CacheId aCacheId)
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
|
|
|
for (uint32_t i = 0; i < mCacheIdRefs.Length(); ++i) {
|
|
|
|
if (mCacheIdRefs[i].mCacheId == aCacheId) {
|
2017-05-26 19:53:35 +03:00
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
2017-01-06 23:41:15 +03:00
|
|
|
uint32_t oldRef = mCacheIdRefs[i].mCount;
|
|
|
|
#endif
|
2015-03-02 16:20:00 +03:00
|
|
|
mCacheIdRefs[i].mCount -= 1;
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mCacheIdRefs[i].mCount < oldRef);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (mCacheIdRefs[i].mCount == 0) {
|
|
|
|
bool orphaned = mCacheIdRefs[i].mOrphaned;
|
|
|
|
mCacheIdRefs.RemoveElementAt(i);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Context> context = mContext;
|
2015-06-26 08:22:46 +03:00
|
|
|
// If the context is already gone, then orphan flag should have been
|
|
|
|
// set in RemoveContext().
|
|
|
|
if (orphaned && context) {
|
|
|
|
if (context->IsCanceled()) {
|
|
|
|
context->NoteOrphanedData();
|
|
|
|
} else {
|
|
|
|
context->CancelForCacheId(aCacheId);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Action> action = new DeleteOrphanedCacheAction(this,
|
2015-06-26 08:22:46 +03:00
|
|
|
aCacheId);
|
|
|
|
context->Dispatch(action);
|
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
}
|
2015-04-03 03:39:46 +03:00
|
|
|
MaybeAllowContextToClose();
|
2015-03-02 16:20:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Attempt to release CacheId that is not referenced!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Manager::AddRefBodyId(const nsID& aBodyId)
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
|
|
|
for (uint32_t i = 0; i < mBodyIdRefs.Length(); ++i) {
|
|
|
|
if (mBodyIdRefs[i].mBodyId == aBodyId) {
|
|
|
|
mBodyIdRefs[i].mCount += 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BodyIdRefCounter* entry = mBodyIdRefs.AppendElement();
|
|
|
|
entry->mBodyId = aBodyId;
|
|
|
|
entry->mCount = 1;
|
|
|
|
entry->mOrphaned = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Manager::ReleaseBodyId(const nsID& aBodyId)
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
|
|
|
for (uint32_t i = 0; i < mBodyIdRefs.Length(); ++i) {
|
|
|
|
if (mBodyIdRefs[i].mBodyId == aBodyId) {
|
2017-05-26 19:53:35 +03:00
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
2017-01-06 23:41:15 +03:00
|
|
|
uint32_t oldRef = mBodyIdRefs[i].mCount;
|
|
|
|
#endif
|
2015-03-02 16:20:00 +03:00
|
|
|
mBodyIdRefs[i].mCount -= 1;
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mBodyIdRefs[i].mCount < oldRef);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (mBodyIdRefs[i].mCount < 1) {
|
|
|
|
bool orphaned = mBodyIdRefs[i].mOrphaned;
|
|
|
|
mBodyIdRefs.RemoveElementAt(i);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Context> context = mContext;
|
2015-06-26 08:22:46 +03:00
|
|
|
// If the context is already gone, then orphan flag should have been
|
|
|
|
// set in RemoveContext().
|
|
|
|
if (orphaned && context) {
|
|
|
|
if (context->IsCanceled()) {
|
|
|
|
context->NoteOrphanedData();
|
|
|
|
} else {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Action> action = new DeleteOrphanedBodyAction(aBodyId);
|
2015-06-26 08:22:46 +03:00
|
|
|
context->Dispatch(action);
|
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
}
|
2015-04-03 03:39:46 +03:00
|
|
|
MaybeAllowContextToClose();
|
2015-03-02 16:20:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Attempt to release BodyId that is not referenced!");
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<ManagerId>
|
|
|
|
Manager::GetManagerId() const
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ManagerId> ref = mManagerId;
|
2015-03-02 16:20:00 +03:00
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Manager::AddStreamList(StreamList* aStreamList)
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aStreamList);
|
2015-03-02 16:20:00 +03:00
|
|
|
mStreamLists.AppendElement(aStreamList);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Manager::RemoveStreamList(StreamList* aStreamList)
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aStreamList);
|
2015-03-02 16:20:00 +03:00
|
|
|
mStreamLists.RemoveElement(aStreamList);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-16 22:00:15 +03:00
|
|
|
Manager::ExecuteCacheOp(Listener* aListener, CacheId aCacheId,
|
|
|
|
const CacheOpArgs& aOpArgs)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aListener);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aOpArgs.type() != CacheOpArgs::TCachePutAllArgs);
|
2015-04-14 00:05:57 +03:00
|
|
|
|
2015-05-01 18:15:52 +03:00
|
|
|
if (NS_WARN_IF(mState == Closing)) {
|
2015-04-16 22:00:15 +03:00
|
|
|
aListener->OnOpComplete(ErrorResult(NS_ERROR_FAILURE), void_t());
|
2015-04-15 10:55:53 +03:00
|
|
|
return;
|
2015-04-15 01:27:48 +03:00
|
|
|
}
|
2015-04-16 22:00:15 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Context> context = mContext;
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!context->IsCanceled());
|
2015-04-16 23:05:38 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<StreamList> streamList = new StreamList(this, context);
|
2015-04-15 10:55:53 +03:00
|
|
|
ListenerId listenerId = SaveListener(aListener);
|
2015-04-14 22:21:14 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Action> action;
|
2015-04-16 22:00:15 +03:00
|
|
|
switch(aOpArgs.type()) {
|
|
|
|
case CacheOpArgs::TCacheMatchArgs:
|
|
|
|
action = new CacheMatchAction(this, listenerId, aCacheId,
|
|
|
|
aOpArgs.get_CacheMatchArgs(), streamList);
|
|
|
|
break;
|
|
|
|
case CacheOpArgs::TCacheMatchAllArgs:
|
|
|
|
action = new CacheMatchAllAction(this, listenerId, aCacheId,
|
|
|
|
aOpArgs.get_CacheMatchAllArgs(),
|
|
|
|
streamList);
|
|
|
|
break;
|
|
|
|
case CacheOpArgs::TCacheDeleteArgs:
|
|
|
|
action = new CacheDeleteAction(this, listenerId, aCacheId,
|
|
|
|
aOpArgs.get_CacheDeleteArgs());
|
|
|
|
break;
|
|
|
|
case CacheOpArgs::TCacheKeysArgs:
|
|
|
|
action = new CacheKeysAction(this, listenerId, aCacheId,
|
|
|
|
aOpArgs.get_CacheKeysArgs(), streamList);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unknown Cache operation!");
|
2015-04-15 10:55:53 +03:00
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-05-07 15:16:51 +03:00
|
|
|
context->Dispatch(action);
|
2015-04-15 10:55:53 +03:00
|
|
|
}
|
2015-04-15 03:11:19 +03:00
|
|
|
|
2015-04-15 10:55:53 +03:00
|
|
|
void
|
2015-04-16 22:00:15 +03:00
|
|
|
Manager::ExecuteStorageOp(Listener* aListener, Namespace aNamespace,
|
|
|
|
const CacheOpArgs& aOpArgs)
|
2015-04-15 10:55:53 +03:00
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aListener);
|
2015-04-14 00:05:57 +03:00
|
|
|
|
2015-05-01 18:15:52 +03:00
|
|
|
if (NS_WARN_IF(mState == Closing)) {
|
2015-04-16 22:00:15 +03:00
|
|
|
aListener->OnOpComplete(ErrorResult(NS_ERROR_FAILURE), void_t());
|
2015-04-15 10:55:53 +03:00
|
|
|
return;
|
2015-04-14 01:18:19 +03:00
|
|
|
}
|
2015-04-16 22:00:15 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Context> context = mContext;
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!context->IsCanceled());
|
2015-04-16 23:05:38 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<StreamList> streamList = new StreamList(this, context);
|
2015-04-15 10:55:53 +03:00
|
|
|
ListenerId listenerId = SaveListener(aListener);
|
2015-04-14 22:21:14 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Action> action;
|
2015-04-16 22:00:15 +03:00
|
|
|
switch(aOpArgs.type()) {
|
|
|
|
case CacheOpArgs::TStorageMatchArgs:
|
|
|
|
action = new StorageMatchAction(this, listenerId, aNamespace,
|
|
|
|
aOpArgs.get_StorageMatchArgs(),
|
|
|
|
streamList);
|
|
|
|
break;
|
|
|
|
case CacheOpArgs::TStorageHasArgs:
|
|
|
|
action = new StorageHasAction(this, listenerId, aNamespace,
|
|
|
|
aOpArgs.get_StorageHasArgs());
|
|
|
|
break;
|
|
|
|
case CacheOpArgs::TStorageOpenArgs:
|
|
|
|
action = new StorageOpenAction(this, listenerId, aNamespace,
|
|
|
|
aOpArgs.get_StorageOpenArgs());
|
|
|
|
break;
|
|
|
|
case CacheOpArgs::TStorageDeleteArgs:
|
|
|
|
action = new StorageDeleteAction(this, listenerId, aNamespace,
|
|
|
|
aOpArgs.get_StorageDeleteArgs());
|
|
|
|
break;
|
|
|
|
case CacheOpArgs::TStorageKeysArgs:
|
|
|
|
action = new StorageKeysAction(this, listenerId, aNamespace);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unknown CacheStorage operation!");
|
2015-04-15 01:27:48 +03:00
|
|
|
}
|
2015-04-15 10:55:53 +03:00
|
|
|
|
2015-05-07 15:16:51 +03:00
|
|
|
context->Dispatch(action);
|
2015-04-15 10:55:53 +03:00
|
|
|
}
|
2015-04-15 03:11:19 +03:00
|
|
|
|
2017-09-15 22:25:40 +03:00
|
|
|
void
|
2017-09-15 22:25:41 +03:00
|
|
|
Manager::ExecuteOpenStream(Listener* aListener, InputStreamResolver&& aResolver,
|
2017-09-15 22:25:40 +03:00
|
|
|
const nsID& aBodyId)
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aListener);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aResolver);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(mState == Closing)) {
|
|
|
|
aResolver(nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Context> context = mContext;
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!context->IsCanceled());
|
|
|
|
|
|
|
|
// We save the listener simply to track the existence of the caller here.
|
|
|
|
// Our returned value will really be passed to the resolver when the
|
|
|
|
// operation completes. In the future we should remove the Listener
|
|
|
|
// mechanism in favor of std::function or MozPromise.
|
|
|
|
ListenerId listenerId = SaveListener(aListener);
|
|
|
|
|
|
|
|
RefPtr<Action> action =
|
2018-05-30 22:15:35 +03:00
|
|
|
new OpenStreamAction(this, listenerId, std::move(aResolver), aBodyId);
|
2017-09-15 22:25:40 +03:00
|
|
|
|
|
|
|
context->Dispatch(action);
|
|
|
|
}
|
|
|
|
|
2015-04-15 10:55:53 +03:00
|
|
|
void
|
2015-04-16 22:00:15 +03:00
|
|
|
Manager::ExecutePutAll(Listener* aListener, CacheId aCacheId,
|
|
|
|
const nsTArray<CacheRequestResponse>& aPutList,
|
|
|
|
const nsTArray<nsCOMPtr<nsIInputStream>>& aRequestStreamList,
|
|
|
|
const nsTArray<nsCOMPtr<nsIInputStream>>& aResponseStreamList)
|
2015-04-15 10:55:53 +03:00
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aListener);
|
2015-04-15 03:11:19 +03:00
|
|
|
|
2015-05-01 18:15:52 +03:00
|
|
|
if (NS_WARN_IF(mState == Closing)) {
|
2015-04-16 22:00:15 +03:00
|
|
|
aListener->OnOpComplete(ErrorResult(NS_ERROR_FAILURE), CachePutAllResult());
|
2015-04-15 10:55:53 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-04-16 22:00:15 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Context> context = mContext;
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!context->IsCanceled());
|
2015-04-16 23:05:38 +03:00
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
ListenerId listenerId = SaveListener(aListener);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Action> action = new CachePutAllAction(this, listenerId, aCacheId,
|
2015-04-16 22:00:15 +03:00
|
|
|
aPutList, aRequestStreamList,
|
|
|
|
aResponseStreamList);
|
|
|
|
|
2015-05-07 15:16:51 +03:00
|
|
|
context->Dispatch(action);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Manager::Manager(ManagerId* aManagerId, nsIThread* aIOThread)
|
|
|
|
: mManagerId(aManagerId)
|
|
|
|
, mIOThread(aIOThread)
|
|
|
|
, mContext(nullptr)
|
|
|
|
, mShuttingDown(false)
|
2015-04-20 21:14:57 +03:00
|
|
|
, mState(Open)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mManagerId);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mIOThread);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Manager::~Manager()
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState == Closing);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mContext);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIThread> ioThread;
|
|
|
|
mIOThread.swap(ioThread);
|
|
|
|
|
|
|
|
// Don't spin the event loop in the destructor waiting for the thread to
|
|
|
|
// shutdown. Defer this to the main thread, instead.
|
2017-06-12 22:34:10 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(NewRunnableMethod("nsIThread::Shutdown",
|
|
|
|
ioThread, &nsIThread::Shutdown)));
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2015-04-16 23:05:38 +03:00
|
|
|
void
|
2015-04-20 21:14:57 +03:00
|
|
|
Manager::Init(Manager* aOldManager)
|
2015-04-16 23:05:38 +03:00
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Context> oldContext;
|
2015-04-20 21:14:57 +03:00
|
|
|
if (aOldManager) {
|
|
|
|
oldContext = aOldManager->mContext;
|
|
|
|
}
|
|
|
|
|
2015-04-16 23:05:38 +03:00
|
|
|
// Create the context immediately. Since there can at most be one Context
|
|
|
|
// per Manager now, this lets us cleanly call Factory::Remove() once the
|
|
|
|
// Context goes away.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Action> setupAction = new SetupAction();
|
2017-06-01 23:42:05 +03:00
|
|
|
RefPtr<Context> ref = Context::Create(this, mIOThread->SerialEventTarget(), setupAction,
|
|
|
|
oldContext);
|
2015-04-16 23:05:38 +03:00
|
|
|
mContext = ref;
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
void
|
|
|
|
Manager::Shutdown()
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
|
|
|
|
|
|
|
// Ignore duplicate attempts to shutdown. This can occur when we start
|
|
|
|
// a browser initiated shutdown and then run ~Manager() which also
|
|
|
|
// calls Shutdown().
|
|
|
|
if (mShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-15 10:54:11 +03:00
|
|
|
mShuttingDown = true;
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-04-16 23:05:38 +03:00
|
|
|
// Note that we are closing to prevent any new requests from coming in and
|
|
|
|
// creating a new Context. We must ensure all Contexts and IO operations are
|
|
|
|
// complete before shutdown proceeds.
|
|
|
|
NoteClosing();
|
|
|
|
|
|
|
|
// If there is a context, then cancel and only note that we are done after
|
|
|
|
// its cleaned up.
|
2015-03-02 16:20:00 +03:00
|
|
|
if (mContext) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Context> context = mContext;
|
2015-03-02 16:20:00 +03:00
|
|
|
context->CancelAll();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-30 15:59:27 +03:00
|
|
|
void
|
|
|
|
Manager::Abort()
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mContext);
|
2015-06-30 15:59:27 +03:00
|
|
|
|
|
|
|
// Note that we are closing to prevent any new requests from coming in and
|
|
|
|
// creating a new Context. We must ensure all Contexts and IO operations are
|
|
|
|
// complete before origin clear proceeds.
|
|
|
|
NoteClosing();
|
|
|
|
|
|
|
|
// Cancel and only note that we are done after the context is cleaned up.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Context> context = mContext;
|
2015-06-30 15:59:27 +03:00
|
|
|
context->CancelAll();
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
Manager::ListenerId
|
|
|
|
Manager::SaveListener(Listener* aListener)
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
|
|
|
|
|
|
|
// Once a Listener is added, we keep a reference to it until its
|
|
|
|
// removed. Since the same Listener might make multiple requests,
|
|
|
|
// ensure we only have a single reference in our list.
|
|
|
|
ListenerList::index_type index =
|
|
|
|
mListeners.IndexOf(aListener, 0, ListenerEntryListenerComparator());
|
|
|
|
if (index != ListenerList::NoIndex) {
|
|
|
|
return mListeners[index].mId;
|
|
|
|
}
|
|
|
|
|
|
|
|
ListenerId id = sNextListenerId;
|
|
|
|
sNextListenerId += 1;
|
|
|
|
|
|
|
|
mListeners.AppendElement(ListenerEntry(id, aListener));
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
Manager::Listener*
|
|
|
|
Manager::GetListener(ListenerId aListenerId) const
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
|
|
|
ListenerList::index_type index =
|
|
|
|
mListeners.IndexOf(aListenerId, 0, ListenerEntryIdComparator());
|
|
|
|
if (index != ListenerList::NoIndex) {
|
|
|
|
return mListeners[index].mListener;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This can legitimately happen if the actor is deleted while a request is
|
|
|
|
// in process. For example, the child process OOMs.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Manager::SetCacheIdOrphanedIfRefed(CacheId aCacheId)
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
|
|
|
for (uint32_t i = 0; i < mCacheIdRefs.Length(); ++i) {
|
|
|
|
if (mCacheIdRefs[i].mCacheId == aCacheId) {
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mCacheIdRefs[i].mCount > 0);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mCacheIdRefs[i].mOrphaned);
|
2015-03-02 16:20:00 +03:00
|
|
|
mCacheIdRefs[i].mOrphaned = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: provide way to set body non-orphaned if its added back to a cache (bug 1110479)
|
|
|
|
|
|
|
|
bool
|
|
|
|
Manager::SetBodyIdOrphanedIfRefed(const nsID& aBodyId)
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
|
|
|
for (uint32_t i = 0; i < mBodyIdRefs.Length(); ++i) {
|
|
|
|
if (mBodyIdRefs[i].mBodyId == aBodyId) {
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mBodyIdRefs[i].mCount > 0);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mBodyIdRefs[i].mOrphaned);
|
2015-03-02 16:20:00 +03:00
|
|
|
mBodyIdRefs[i].mOrphaned = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Manager::NoteOrphanedBodyIdList(const nsTArray<nsID>& aDeletedBodyIdList)
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<nsID, 64> deleteNowList;
|
2015-03-02 16:20:00 +03:00
|
|
|
deleteNowList.SetCapacity(aDeletedBodyIdList.Length());
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aDeletedBodyIdList.Length(); ++i) {
|
|
|
|
if (!SetBodyIdOrphanedIfRefed(aDeletedBodyIdList[i])) {
|
|
|
|
deleteNowList.AppendElement(aDeletedBodyIdList[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 23:05:38 +03:00
|
|
|
// TODO: note that we need to check these bodies for staleness on startup (bug 1110446)
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Context> context = mContext;
|
2015-04-16 23:05:38 +03:00
|
|
|
if (!deleteNowList.IsEmpty() && context && !context->IsCanceled()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Action> action = new DeleteOrphanedBodyAction(deleteNowList);
|
2015-05-07 15:16:51 +03:00
|
|
|
context->Dispatch(action);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 03:39:46 +03:00
|
|
|
void
|
|
|
|
Manager::MaybeAllowContextToClose()
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Manager);
|
|
|
|
|
|
|
|
// If we have an active context, but we have no more users of the Manager,
|
|
|
|
// then let it shut itself down. We must wait for all possible users of
|
|
|
|
// Cache state information to complete before doing this. Once we allow
|
|
|
|
// the Context to close we may not reliably get notified of storage
|
|
|
|
// invalidation.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Context> context = mContext;
|
2015-04-16 23:05:38 +03:00
|
|
|
if (context && mListeners.IsEmpty()
|
|
|
|
&& mCacheIdRefs.IsEmpty()
|
|
|
|
&& mBodyIdRefs.IsEmpty()) {
|
2015-04-03 03:39:46 +03:00
|
|
|
|
|
|
|
// Mark this Manager as invalid so that it won't get used again. We don't
|
|
|
|
// want to start any new operations once we allow the Context to close since
|
|
|
|
// it may race with the underlying storage getting invalidated.
|
2015-04-16 23:05:38 +03:00
|
|
|
NoteClosing();
|
2015-04-03 03:39:46 +03:00
|
|
|
|
2015-04-16 23:05:38 +03:00
|
|
|
context->AllowToClose();
|
2015-04-03 03:39:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
} // namespace cache
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|