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/CacheStreamControlChild.h"
|
|
|
|
|
|
|
|
#include "mozilla/DebugOnly.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"
|
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/ReadStream.h"
|
2015-03-20 21:01:57 +03:00
|
|
|
#include "mozilla/ipc/FileDescriptorSetChild.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundChild.h"
|
|
|
|
#include "mozilla/ipc/PFileDescriptorSetChild.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace cache {
|
|
|
|
|
2016-05-18 01:01:25 +03:00
|
|
|
using mozilla::dom::OptionalFileDescriptorSet;
|
2016-05-15 20:32:09 +03:00
|
|
|
using mozilla::ipc::AutoIPCStream;
|
2015-03-20 21:01:57 +03:00
|
|
|
using mozilla::ipc::FileDescriptor;
|
|
|
|
using mozilla::ipc::FileDescriptorSetChild;
|
|
|
|
using mozilla::ipc::PFileDescriptorSetChild;
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
// declared in ActorUtils.h
|
|
|
|
PCacheStreamControlChild*
|
|
|
|
AllocPCacheStreamControlChild()
|
|
|
|
{
|
|
|
|
return new CacheStreamControlChild();
|
|
|
|
}
|
|
|
|
|
|
|
|
// declared in ActorUtils.h
|
|
|
|
void
|
|
|
|
DeallocPCacheStreamControlChild(PCacheStreamControlChild* aActor)
|
|
|
|
{
|
|
|
|
delete aActor;
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheStreamControlChild::CacheStreamControlChild()
|
|
|
|
: mDestroyStarted(false)
|
2015-04-16 22:00:15 +03:00
|
|
|
, mDestroyDelayed(false)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(cache::CacheStreamControlChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheStreamControlChild::~CacheStreamControlChild()
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
|
|
|
|
MOZ_COUNT_DTOR(cache::CacheStreamControlChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-03-20 21:01:57 +03:00
|
|
|
CacheStreamControlChild::StartDestroy()
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
|
2015-03-20 21:01:57 +03:00
|
|
|
// This can get called twice under some circumstances. For example, if the
|
2016-06-23 11:53:14 +03:00
|
|
|
// actor is added to a CacheWorkerHolder that has already been notified and
|
|
|
|
// the Cache actor has no mListener.
|
2015-03-20 21:01:57 +03:00
|
|
|
if (mDestroyStarted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mDestroyStarted = true;
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
// If any of the streams have started to be read, then wait for them to close
|
|
|
|
// naturally.
|
|
|
|
if (HasEverBeenRead()) {
|
|
|
|
// Note that we are delaying so that we can re-check for active streams
|
|
|
|
// in NoteClosedAfterForget().
|
|
|
|
mDestroyDelayed = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, if the streams have not been touched then just pre-emptively
|
|
|
|
// close them now. This handles the case where someone retrieves a Response
|
|
|
|
// from the Cache, but never accesses the body. We should not keep the
|
|
|
|
// Worker alive until that Response is GC'd just because of its ignored
|
|
|
|
// body stream.
|
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
// Begin shutting down all streams. This is the same as if the parent had
|
|
|
|
// asked us to shutdown. So simulate the CloseAll IPC message.
|
|
|
|
RecvCloseAll();
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-16 22:00:15 +03:00
|
|
|
CacheStreamControlChild::SerializeControl(CacheReadStream* aReadStreamOut)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
|
2015-03-20 21:01:57 +03:00
|
|
|
aReadStreamOut->controlParent() = nullptr;
|
|
|
|
aReadStreamOut->controlChild() = this;
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-15 20:32:09 +03:00
|
|
|
CacheStreamControlChild::SerializeStream(CacheReadStream* aReadStreamOut,
|
|
|
|
nsIInputStream* aStream,
|
|
|
|
nsTArray<UniquePtr<AutoIPCStream>>& aStreamCleanupList)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
|
2016-05-15 20:32:09 +03:00
|
|
|
MOZ_ASSERT(aReadStreamOut);
|
|
|
|
MOZ_ASSERT(aStream);
|
|
|
|
UniquePtr<AutoIPCStream> autoStream(new AutoIPCStream(aReadStreamOut->stream()));
|
|
|
|
autoStream->Serialize(aStream, Manager());
|
|
|
|
aStreamCleanupList.AppendElement(Move(autoStream));
|
2015-03-20 21:01:57 +03:00
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
void
|
2015-03-20 21:01:57 +03:00
|
|
|
CacheStreamControlChild::NoteClosedAfterForget(const nsID& aId)
|
2015-03-20 21:01:57 +03:00
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << SendNoteClosed(aId);
|
2015-04-16 22:00:15 +03:00
|
|
|
|
|
|
|
// A stream has closed. If we delayed StartDestry() due to this stream
|
|
|
|
// being read, then we should check to see if any of the remaining streams
|
|
|
|
// are active. If none of our other streams have been read, then we can
|
|
|
|
// proceed with the shutdown now.
|
|
|
|
if (mDestroyDelayed && !HasEverBeenRead()) {
|
|
|
|
mDestroyDelayed = false;
|
|
|
|
RecvCloseAll();
|
|
|
|
}
|
2015-03-20 21:01:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
|
|
|
CacheStreamControlChild::AssertOwningThread()
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
|
|
|
|
}
|
|
|
|
#endif
|
2015-03-21 00:03:27 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
void
|
|
|
|
CacheStreamControlChild::ActorDestroy(ActorDestroyReason aReason)
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
|
|
|
|
CloseAllReadStreamsWithoutReporting();
|
2016-06-23 11:53:14 +03:00
|
|
|
RemoveWorkerHolder();
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CacheStreamControlChild::RecvClose(const nsID& aId)
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
|
2015-03-20 21:01:57 +03:00
|
|
|
CloseReadStreams(aId);
|
2015-03-02 16:20:00 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CacheStreamControlChild::RecvCloseAll()
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheStreamControlChild);
|
2015-03-20 21:01:57 +03:00
|
|
|
CloseAllReadStreams();
|
2015-03-02 16:20:00 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cache
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|