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/CacheChild.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/ActorUtils.h"
|
|
|
|
#include "mozilla/dom/cache/Cache.h"
|
2015-04-16 22:00:15 +03:00
|
|
|
#include "mozilla/dom/cache/CacheOpChild.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace cache {
|
|
|
|
|
|
|
|
// Declared in ActorUtils.h
|
|
|
|
PCacheChild* AllocPCacheChild() { return new CacheChild(); }
|
|
|
|
|
|
|
|
// Declared in ActorUtils.h
|
|
|
|
void DeallocPCacheChild(PCacheChild* aActor) { delete aActor; }
|
|
|
|
|
|
|
|
CacheChild::CacheChild()
|
|
|
|
: mListener(nullptr),
|
2015-04-16 22:00:15 +03:00
|
|
|
mNumChildActors(0),
|
2015-05-01 18:13:36 +03:00
|
|
|
mDelayedDestroy(false),
|
2016-01-04 23:03:11 +03:00
|
|
|
mLocked(false) {
|
2015-03-02 16:20:00 +03:00
|
|
|
MOZ_COUNT_CTOR(cache::CacheChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheChild::~CacheChild() {
|
|
|
|
MOZ_COUNT_DTOR(cache::CacheChild);
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheChild);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mListener);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mNumChildActors);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mLocked);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CacheChild::SetListener(Cache* aListener) {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheChild);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mListener);
|
2015-03-02 16:20:00 +03:00
|
|
|
mListener = aListener;
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mListener);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CacheChild::ClearListener() {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheChild);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mListener);
|
2015-03-02 16:20:00 +03:00
|
|
|
mListener = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
void CacheChild::ExecuteOp(nsIGlobalObject* aGlobal, Promise* aPromise,
|
2015-05-01 18:13:36 +03:00
|
|
|
nsISupports* aParent, const CacheOpArgs& aArgs) {
|
2015-04-16 22:00:15 +03:00
|
|
|
mNumChildActors += 1;
|
|
|
|
MOZ_ALWAYS_TRUE(SendPCacheOpConstructor(
|
2016-06-23 11:53:14 +03:00
|
|
|
new CacheOpChild(GetWorkerHolder(), aGlobal, aParent, aPromise), aArgs));
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
|
2015-05-01 18:13:36 +03:00
|
|
|
void CacheChild::StartDestroyFromListener() {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheChild);
|
|
|
|
|
|
|
|
// The listener should be held alive by any async operations, so if it
|
|
|
|
// is going away then there must not be any child actors. This in turn
|
|
|
|
// ensures that StartDestroy() will not trigger the delayed path.
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mNumChildActors);
|
2015-05-01 18:13:36 +03:00
|
|
|
|
|
|
|
StartDestroy();
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
void CacheChild::StartDestroy() {
|
2015-05-01 18:13:36 +03:00
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheChild);
|
|
|
|
|
|
|
|
// If we have outstanding child actors, then don't destroy ourself yet.
|
|
|
|
// The child actors should be short lived and we should allow them to complete
|
|
|
|
// if possible. NoteDeletedActor() will call back into this Shutdown()
|
2016-01-04 23:03:11 +03:00
|
|
|
// method when the last child actor is gone. Also, delay destruction if we
|
|
|
|
// have been explicitly locked by someone using us on the stack.
|
|
|
|
if (mNumChildActors || mLocked) {
|
2015-05-01 18:13:36 +03:00
|
|
|
mDelayedDestroy = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Cache> listener = mListener;
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
// StartDestroy() can get called from either Cache or the WorkerHolder.
|
2015-03-02 16:20:00 +03:00
|
|
|
// Theoretically we can get double called if the right race happens. Handle
|
|
|
|
// that by just ignoring the second StartDestroy() call.
|
|
|
|
if (!listener) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
listener->DestroyInternal(this);
|
|
|
|
|
|
|
|
// Cache listener should call ClearListener() in DestroyInternal()
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mListener);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
// Start actor destruction from parent process
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << SendTeardown();
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CacheChild::ActorDestroy(ActorDestroyReason aReason) {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheChild);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Cache> listener = mListener;
|
2015-03-02 16:20:00 +03:00
|
|
|
if (listener) {
|
|
|
|
listener->DestroyInternal(this);
|
|
|
|
// Cache listener should call ClearListener() in DestroyInternal()
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mListener);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2016-06-23 11:53:14 +03:00
|
|
|
RemoveWorkerHolder();
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
PCacheOpChild* CacheChild::AllocPCacheOpChild(const CacheOpArgs& aOpArgs) {
|
|
|
|
MOZ_CRASH("CacheOpChild should be manually constructed.");
|
2015-03-22 09:52:12 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
bool CacheChild::DeallocPCacheOpChild(PCacheOpChild* aActor) {
|
2015-03-22 09:52:12 +03:00
|
|
|
delete aActor;
|
2015-04-16 22:00:15 +03:00
|
|
|
NoteDeletedActor();
|
2015-03-22 09:52:12 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
void CacheChild::NoteDeletedActor() {
|
|
|
|
mNumChildActors -= 1;
|
2016-01-04 23:03:11 +03:00
|
|
|
MaybeFlushDelayedDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheChild::MaybeFlushDelayedDestroy() {
|
|
|
|
if (!mNumChildActors && !mLocked && mDelayedDestroy) {
|
2015-05-01 18:13:36 +03:00
|
|
|
StartDestroy();
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-04 23:03:11 +03:00
|
|
|
void CacheChild::Lock() {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheChild);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mLocked);
|
2016-01-04 23:03:11 +03:00
|
|
|
mLocked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheChild::Unlock() {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheChild);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mLocked);
|
2016-01-04 23:03:11 +03:00
|
|
|
mLocked = false;
|
|
|
|
MaybeFlushDelayedDestroy();
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
} // namespace cache
|
|
|
|
} // namespace dom
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|