2017-06-20 18:53:21 +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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_FetchConsumer_h
|
|
|
|
#define mozilla_dom_FetchConsumer_h
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
#include "Fetch.h"
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
class nsIThread;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
class Promise;
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
namespace workers {
|
|
|
|
class WorkerPrivate;
|
|
|
|
class WorkerHolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Derived> class FetchBody;
|
|
|
|
|
|
|
|
// FetchBody is not thread-safe but we need to move it around threads.
|
|
|
|
// In order to keep it alive all the time, we use a WorkerHolder, if created on
|
2017-06-20 18:53:21 +03:00
|
|
|
// workers, plus a this consumer.
|
2017-06-20 18:53:21 +03:00
|
|
|
template <class Derived>
|
2017-06-20 18:53:21 +03:00
|
|
|
class FetchBodyConsumer final
|
2017-06-20 18:53:21 +03:00
|
|
|
{
|
|
|
|
public:
|
2017-06-20 18:53:21 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FetchBodyConsumer<Derived>)
|
2017-06-20 18:53:21 +03:00
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
static already_AddRefed<Promise>
|
|
|
|
Create(nsIGlobalObject* aGlobal,
|
|
|
|
nsIEventTarget* aMainThreadEventTarget,
|
|
|
|
FetchBody<Derived>* aBody,
|
|
|
|
FetchConsumeType aType,
|
|
|
|
ErrorResult& aRv);
|
2017-06-20 18:53:21 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
ReleaseObject();
|
|
|
|
|
|
|
|
FetchBody<Derived>*
|
|
|
|
Body() const
|
|
|
|
{
|
|
|
|
return mBody;
|
|
|
|
}
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
void
|
|
|
|
BeginConsumeBodyMainThread();
|
|
|
|
|
|
|
|
void
|
|
|
|
ContinueConsumeBody(nsresult aStatus, uint32_t aLength, uint8_t* aResult);
|
|
|
|
|
|
|
|
void
|
|
|
|
ContinueConsumeBlobBody(BlobImpl* aBlobImpl);
|
|
|
|
|
|
|
|
void
|
|
|
|
CancelPump();
|
|
|
|
|
|
|
|
workers::WorkerPrivate*
|
|
|
|
GetWorkerPrivate() const
|
|
|
|
{
|
|
|
|
return mWorkerPrivate;
|
|
|
|
}
|
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
private:
|
2017-06-20 18:53:21 +03:00
|
|
|
FetchBodyConsumer(nsIEventTarget* aMainThreadEventTarget,
|
|
|
|
workers::WorkerPrivate* aWorkerPrivate,
|
|
|
|
FetchBody<Derived>* aBody,
|
|
|
|
Promise* aPromise,
|
|
|
|
FetchConsumeType aType);
|
2017-06-20 18:53:21 +03:00
|
|
|
|
2017-06-20 18:53:21 +03:00
|
|
|
~FetchBodyConsumer();
|
2017-06-20 18:53:21 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
AssertIsOnTargetThread() const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
RegisterWorkerHolder(workers::WorkerPrivate* aWorkerPrivate);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIThread> mTargetThread;
|
2017-06-20 18:53:21 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> mMainThreadEventTarget;
|
2017-06-20 18:53:21 +03:00
|
|
|
RefPtr<FetchBody<Derived>> mBody;
|
|
|
|
|
|
|
|
// Set when consuming the body is attempted on a worker.
|
|
|
|
// Unset when consumption is done/aborted.
|
2017-06-20 18:53:21 +03:00
|
|
|
// This WorkerHolder keeps alive the consumer via a cycle.
|
2017-06-20 18:53:21 +03:00
|
|
|
UniquePtr<workers::WorkerHolder> mWorkerHolder;
|
2017-06-20 18:53:21 +03:00
|
|
|
|
|
|
|
// Always set whenever the FetchBodyConsumer is created on the worker thread.
|
|
|
|
workers::WorkerPrivate* mWorkerPrivate;
|
|
|
|
|
|
|
|
nsMainThreadPtrHandle<nsIInputStreamPump> mConsumeBodyPump;
|
|
|
|
|
|
|
|
// Only ever set once, always on target thread.
|
|
|
|
FetchConsumeType mConsumeType;
|
|
|
|
RefPtr<Promise> mConsumePromise;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool mReadDone;
|
|
|
|
#endif
|
2017-06-20 18:53:21 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_FetchConsumer_h
|