Skip to content

Commit

Permalink
Bug 1110814 P1 Implement Cache IPC actor for streaming data from chil…
Browse files Browse the repository at this point in the history
…d to parent. r=khuey
  • Loading branch information
wanderview committed Mar 22, 2015
1 parent 6d7c90f commit 1d3b05b
Show file tree
Hide file tree
Showing 31 changed files with 788 additions and 77 deletions.
60 changes: 44 additions & 16 deletions dom/cache/AutoUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "mozilla/dom/cache/AutoUtils.h"

#include "mozilla/unused.h"
#include "mozilla/dom/cache/CachePushStreamChild.h"
#include "mozilla/dom/cache/CacheStreamControlParent.h"
#include "mozilla/dom/cache/ReadStream.h"
#include "mozilla/dom/cache/SavedTypes.h"
Expand All @@ -19,6 +20,7 @@
namespace {

using mozilla::unused;
using mozilla::dom::cache::CachePushStreamChild;
using mozilla::dom::cache::PCacheReadStream;
using mozilla::dom::cache::PCacheReadStreamOrVoid;
using mozilla::ipc::FileDescriptor;
Expand All @@ -28,8 +30,8 @@ using mozilla::ipc::OptionalFileDescriptorSet;

enum CleanupAction
{
ForgetFds,
DeleteFds
Forget,
Delete
};

void
Expand All @@ -46,7 +48,7 @@ CleanupChildFds(PCacheReadStream& aReadStream, CleanupAction aAction)
static_cast<FileDescriptorSetChild*>(aReadStream.fds().get_PFileDescriptorSetChild());
MOZ_ASSERT(fdSetActor);

if (aAction == DeleteFds) {
if (aAction == Delete) {
unused << fdSetActor->Send__delete__(fdSetActor);
}

Expand All @@ -57,13 +59,39 @@ CleanupChildFds(PCacheReadStream& aReadStream, CleanupAction aAction)
}

void
CleanupChildFds(PCacheReadStreamOrVoid& aReadStreamOrVoid, CleanupAction aAction)
CleanupChildPushStream(PCacheReadStream& aReadStream, CleanupAction aAction)
{
if (!aReadStream.pushStreamChild()) {
return;
}

auto pushStream =
static_cast<CachePushStreamChild*>(aReadStream.pushStreamChild());

if (aAction == Delete) {
pushStream->StartDestroy();
return;
}

// If we send the stream, then we need to start it before forgetting about it.
pushStream->Start();
}

void
CleanupChild(PCacheReadStream& aReadStream, CleanupAction aAction)
{
CleanupChildFds(aReadStream, aAction);
CleanupChildPushStream(aReadStream, aAction);
}

void
CleanupChild(PCacheReadStreamOrVoid& aReadStreamOrVoid, CleanupAction aAction)
{
if (aReadStreamOrVoid.type() == PCacheReadStreamOrVoid::Tvoid_t) {
return;
}

CleanupChildFds(aReadStreamOrVoid.get_PCacheReadStream(), aAction);
CleanupChild(aReadStreamOrVoid.get_PCacheReadStream(), aAction);
}

void
Expand All @@ -80,7 +108,7 @@ CleanupParentFds(PCacheReadStream& aReadStream, CleanupAction aAction)
static_cast<FileDescriptorSetParent*>(aReadStream.fds().get_PFileDescriptorSetParent());
MOZ_ASSERT(fdSetActor);

if (aAction == DeleteFds) {
if (aAction == Delete) {
unused << fdSetActor->Send__delete__(fdSetActor);
}

Expand Down Expand Up @@ -133,8 +161,8 @@ AutoChildRequest::~AutoChildRequest()
return;
}

CleanupAction action = mSent ? ForgetFds : DeleteFds;
CleanupChildFds(mRequestOrVoid.get_PCacheRequest().body(), action);
CleanupAction action = mSent ? Forget : Delete;
CleanupChild(mRequestOrVoid.get_PCacheRequest().body(), action);
}

void
Expand Down Expand Up @@ -173,9 +201,9 @@ AutoChildRequestList::AutoChildRequestList(TypeUtils* aTypeUtils,

AutoChildRequestList::~AutoChildRequestList()
{
CleanupAction action = mSent ? ForgetFds : DeleteFds;
CleanupAction action = mSent ? Forget : Delete;
for (uint32_t i = 0; i < mRequestList.Length(); ++i) {
CleanupChildFds(mRequestList[i].body(), action);
CleanupChild(mRequestList[i].body(), action);
}
}

Expand Down Expand Up @@ -223,9 +251,9 @@ AutoChildRequestResponse::AutoChildRequestResponse(TypeUtils* aTypeUtils)

AutoChildRequestResponse::~AutoChildRequestResponse()
{
CleanupAction action = mSent ? ForgetFds : DeleteFds;
CleanupChildFds(mRequestResponse.request().body(), action);
CleanupChildFds(mRequestResponse.response().body(), action);
CleanupAction action = mSent ? Forget : Delete;
CleanupChild(mRequestResponse.request().body(), action);
CleanupChild(mRequestResponse.response().body(), action);
}

void
Expand Down Expand Up @@ -311,7 +339,7 @@ AutoParentRequestList::AutoParentRequestList(PBackgroundParent* aManager,

AutoParentRequestList::~AutoParentRequestList()
{
CleanupAction action = mSent ? ForgetFds : DeleteFds;
CleanupAction action = mSent ? Forget : Delete;
for (uint32_t i = 0; i < mRequestList.Length(); ++i) {
CleanupParentFds(mRequestList[i].body(), action);
}
Expand Down Expand Up @@ -355,7 +383,7 @@ AutoParentResponseList::AutoParentResponseList(PBackgroundParent* aManager,

AutoParentResponseList::~AutoParentResponseList()
{
CleanupAction action = mSent ? ForgetFds : DeleteFds;
CleanupAction action = mSent ? Forget : Delete;
for (uint32_t i = 0; i < mResponseList.Length(); ++i) {
CleanupParentFds(mResponseList[i].body(), action);
}
Expand Down Expand Up @@ -402,7 +430,7 @@ AutoParentResponseOrVoid::~AutoParentResponseOrVoid()
return;
}

CleanupAction action = mSent ? ForgetFds : DeleteFds;
CleanupAction action = mSent ? Forget : Delete;
CleanupParentFds(mResponseOrVoid.get_PCacheResponse().body(), action);
}

Expand Down
13 changes: 13 additions & 0 deletions dom/cache/Cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "mozilla/dom/CacheBinding.h"
#include "mozilla/dom/cache/AutoUtils.h"
#include "mozilla/dom/cache/CacheChild.h"
#include "mozilla/dom/cache/CachePushStreamChild.h"
#include "mozilla/dom/cache/ReadStream.h"
#include "mozilla/dom/cache/TypeUtils.h"
#include "mozilla/ErrorResult.h"
Expand Down Expand Up @@ -526,6 +527,18 @@ Cache::AssertOwningThread() const
}
#endif

CachePushStreamChild*
Cache::CreatePushStream(nsIAsyncInputStream* aStream)
{
NS_ASSERT_OWNINGTHREAD(Cache);
MOZ_ASSERT(mActor);
MOZ_ASSERT(aStream);
auto actor = mActor->SendPCachePushStreamConstructor(
new CachePushStreamChild(mActor->GetFeature(), aStream));
MOZ_ASSERT(actor);
return static_cast<CachePushStreamChild*>(actor);
}

void
Cache::ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue)
{
Expand Down
7 changes: 5 additions & 2 deletions dom/cache/Cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class PCacheResponse;
class PCacheResponseOrVoid;

class Cache final : public PromiseNativeHandler
, public nsWrapperCache
, public TypeUtils
, public nsWrapperCache
, public TypeUtils
{
public:
Cache(nsIGlobalObject* aGlobal, CacheChild* aActor);
Expand Down Expand Up @@ -97,6 +97,9 @@ class Cache final : public PromiseNativeHandler
virtual void AssertOwningThread() const override;
#endif

virtual CachePushStreamChild*
CreatePushStream(nsIAsyncInputStream* aStream) override;

// PromiseNativeHandler methods
virtual void
ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
Expand Down
15 changes: 15 additions & 0 deletions dom/cache/CacheChild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "mozilla/unused.h"
#include "mozilla/dom/cache/ActorUtils.h"
#include "mozilla/dom/cache/Cache.h"
#include "mozilla/dom/cache/PCachePushStreamChild.h"
#include "mozilla/dom/cache/StreamUtils.h"

namespace mozilla {
Expand Down Expand Up @@ -94,6 +95,20 @@ CacheChild::ActorDestroy(ActorDestroyReason aReason)
RemoveFeature();
}

PCachePushStreamChild*
CacheChild::AllocPCachePushStreamChild()
{
MOZ_CRASH("CachePushStreamChild should be manually constructed.");
return nullptr;
}

bool
CacheChild::DeallocPCachePushStreamChild(PCachePushStreamChild* aActor)
{
delete aActor;
return true;
}

bool
CacheChild::RecvMatchResponse(const RequestId& requestId, const nsresult& aRv,
const PCacheResponseOrVoid& aResponse)
Expand Down
8 changes: 7 additions & 1 deletion dom/cache/CacheChild.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace cache {
class Cache;

class CacheChild final : public PCacheChild
, public ActorChild
, public ActorChild
{
public:
CacheChild();
Expand All @@ -41,6 +41,12 @@ class CacheChild final : public PCacheChild
virtual void
ActorDestroy(ActorDestroyReason aReason) override;

virtual PCachePushStreamChild*
AllocPCachePushStreamChild() override;

virtual bool
DeallocPCachePushStreamChild(PCachePushStreamChild* aActor) override;

virtual bool
RecvMatchResponse(const RequestId& requestId, const nsresult& aRv,
const PCacheResponseOrVoid& aResponse) override;
Expand Down
30 changes: 29 additions & 1 deletion dom/cache/CacheParent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "mozilla/DebugOnly.h"
#include "mozilla/dom/cache/AutoUtils.h"
#include "mozilla/dom/cache/CachePushStreamParent.h"
#include "mozilla/dom/cache/CacheStreamControlParent.h"
#include "mozilla/dom/cache/ReadStream.h"
#include "mozilla/dom/cache/SavedTypes.h"
Expand Down Expand Up @@ -61,6 +62,19 @@ CacheParent::ActorDestroy(ActorDestroyReason aReason)
mManager = nullptr;
}

PCachePushStreamParent*
CacheParent::AllocPCachePushStreamParent()
{
return CachePushStreamParent::Create();
}

bool
CacheParent::DeallocPCachePushStreamParent(PCachePushStreamParent* aActor)
{
delete aActor;
return true;
}

bool
CacheParent::RecvTeardown()
{
Expand Down Expand Up @@ -259,13 +273,27 @@ CacheParent::DeserializeCacheStream(const PCacheReadStreamOrVoid& aStreamOrVoid)
return nullptr;
}

nsCOMPtr<nsIInputStream> stream;
const PCacheReadStream& readStream = aStreamOrVoid.get_PCacheReadStream();

nsCOMPtr<nsIInputStream> stream = ReadStream::Create(readStream);
// Option 1: A push stream actor was sent for nsPipe data
if (readStream.pushStreamParent()) {
MOZ_ASSERT(!readStream.controlParent());
CachePushStreamParent* pushStream =
static_cast<CachePushStreamParent*>(readStream.pushStreamParent());
stream = pushStream->TakeReader();
MOZ_ASSERT(stream);
return stream.forget();
}

// Option 2: One of our own ReadStreams was passed back to us with a stream
// control actor.
stream = ReadStream::Create(readStream);
if (stream) {
return stream.forget();
}

// Option 3: A stream was serialized using normal methods.
nsAutoTArray<FileDescriptor, 4> fds;
if (readStream.fds().type() ==
OptionalFileDescriptorSet::TPFileDescriptorSetChild) {
Expand Down
6 changes: 4 additions & 2 deletions dom/cache/CacheParent.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ namespace cache {
struct SavedResponse;

class CacheParent final : public PCacheParent
, public Manager::Listener
, public FetchPut::Listener
, public Manager::Listener
, public FetchPut::Listener
{
public:
CacheParent(cache::Manager* aManager, CacheId aCacheId);
Expand All @@ -32,6 +32,8 @@ class CacheParent final : public PCacheParent
private:
// PCacheParent method
virtual void ActorDestroy(ActorDestroyReason aReason) override;
virtual PCachePushStreamParent* AllocPCachePushStreamParent();
virtual bool DeallocPCachePushStreamParent(PCachePushStreamParent* aActor);
virtual bool RecvTeardown() override;
virtual bool
RecvMatch(const RequestId& aRequestId, const PCacheRequest& aRequest,
Expand Down
Loading

0 comments on commit 1d3b05b

Please sign in to comment.