2017-06-20 15:53:21 +00: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 15:53:21 +00:00
|
|
|
#include "Fetch.h"
|
2017-08-29 09:31:06 +00:00
|
|
|
#include "mozilla/dom/AbortSignal.h"
|
2017-09-04 15:06:18 +00:00
|
|
|
#include "mozilla/dom/MutableBlobStorage.h"
|
2017-06-22 11:16:32 +00:00
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsWeakReference.h"
|
2017-06-20 15:53:21 +00:00
|
|
|
|
2017-06-20 15:53:21 +00:00
|
|
|
class nsIThread;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2017-06-20 15:53:21 +00:00
|
|
|
class Promise;
|
2018-03-16 15:52:28 +00:00
|
|
|
class ThreadSafeWorkerRef;
|
2017-06-20 15:53:21 +00:00
|
|
|
|
|
|
|
template <class Derived> class FetchBody;
|
|
|
|
|
2018-03-16 15:52:28 +00:00
|
|
|
// 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 ThreadSafeWorkerRef, if created on
|
|
|
|
// workers.
|
2017-06-20 15:53:21 +00:00
|
|
|
template <class Derived>
|
2017-06-22 11:16:32 +00:00
|
|
|
class FetchBodyConsumer final : public nsIObserver
|
|
|
|
, public nsSupportsWeakReference
|
2017-08-29 09:31:07 +00:00
|
|
|
, public AbortFollower
|
2017-06-20 15:53:21 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-06-22 11:16:32 +00:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
2017-06-20 15:53:21 +00:00
|
|
|
|
2017-06-20 15:53:21 +00:00
|
|
|
static already_AddRefed<Promise>
|
|
|
|
Create(nsIGlobalObject* aGlobal,
|
|
|
|
nsIEventTarget* aMainThreadEventTarget,
|
|
|
|
FetchBody<Derived>* aBody,
|
2018-11-06 13:48:07 +00:00
|
|
|
nsIInputStream* aBodyStream,
|
2018-08-26 12:16:21 +00:00
|
|
|
AbortSignalImpl* aSignalImpl,
|
2017-06-20 15:53:21 +00:00
|
|
|
FetchConsumeType aType,
|
|
|
|
ErrorResult& aRv);
|
2017-06-20 15:53:21 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
ReleaseObject();
|
|
|
|
|
2017-06-20 15:53:21 +00:00
|
|
|
void
|
2018-03-16 15:52:28 +00:00
|
|
|
BeginConsumeBodyMainThread(ThreadSafeWorkerRef* aWorkerRef);
|
2017-06-20 15:53:21 +00:00
|
|
|
|
2018-09-11 19:13:15 +00:00
|
|
|
void
|
|
|
|
OnBlobResult(Blob* aBlob, ThreadSafeWorkerRef* aWorkerRef = nullptr);
|
|
|
|
|
2017-06-20 15:53:21 +00:00
|
|
|
void
|
2018-02-05 18:55:08 +00:00
|
|
|
ContinueConsumeBody(nsresult aStatus, uint32_t aLength, uint8_t* aResult,
|
|
|
|
bool aShuttingDown = false);
|
2017-06-20 15:53:21 +00:00
|
|
|
|
|
|
|
void
|
2018-02-05 18:55:08 +00:00
|
|
|
ContinueConsumeBlobBody(BlobImpl* aBlobImpl, bool aShuttingDown = false);
|
2017-06-20 15:53:21 +00:00
|
|
|
|
|
|
|
void
|
2017-07-10 20:07:44 +00:00
|
|
|
ShutDownMainThreadConsuming();
|
2017-06-20 15:53:21 +00:00
|
|
|
|
2017-07-10 20:07:44 +00:00
|
|
|
void
|
|
|
|
NullifyConsumeBodyPump()
|
|
|
|
{
|
2017-09-04 15:06:47 +00:00
|
|
|
mShuttingDown = true;
|
2017-07-10 20:07:44 +00:00
|
|
|
mConsumeBodyPump = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-29 09:31:07 +00:00
|
|
|
// AbortFollower
|
|
|
|
void Abort() override;
|
2017-08-29 09:31:06 +00:00
|
|
|
|
2017-06-20 15:53:21 +00:00
|
|
|
private:
|
2017-06-20 15:53:21 +00:00
|
|
|
FetchBodyConsumer(nsIEventTarget* aMainThreadEventTarget,
|
2017-06-22 11:16:32 +00:00
|
|
|
nsIGlobalObject* aGlobalObject,
|
2017-06-20 15:53:21 +00:00
|
|
|
FetchBody<Derived>* aBody,
|
2017-09-04 15:06:18 +00:00
|
|
|
nsIInputStream* aBodyStream,
|
2017-06-20 15:53:21 +00:00
|
|
|
Promise* aPromise,
|
|
|
|
FetchConsumeType aType);
|
2017-06-20 15:53:21 +00:00
|
|
|
|
2017-06-20 15:53:21 +00:00
|
|
|
~FetchBodyConsumer();
|
2017-06-20 15:53:21 +00:00
|
|
|
|
2018-09-11 19:13:15 +00:00
|
|
|
nsresult
|
|
|
|
GetBodyLocalFile(nsIFile** aFile) const;
|
|
|
|
|
2017-06-20 15:53:21 +00:00
|
|
|
void
|
|
|
|
AssertIsOnTargetThread() const;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIThread> mTargetThread;
|
2017-06-20 15:53:21 +00:00
|
|
|
nsCOMPtr<nsIEventTarget> mMainThreadEventTarget;
|
2017-09-04 15:06:18 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// This is used only to check if the body has been correctly consumed.
|
2017-06-20 15:53:21 +00:00
|
|
|
RefPtr<FetchBody<Derived>> mBody;
|
2017-09-04 15:06:18 +00:00
|
|
|
#endif
|
|
|
|
|
2017-11-22 10:18:57 +00:00
|
|
|
// This is nullified when the consuming of the body starts.
|
2017-09-04 15:06:18 +00:00
|
|
|
nsCOMPtr<nsIInputStream> mBodyStream;
|
2017-11-22 10:18:57 +00:00
|
|
|
|
2017-09-04 15:06:18 +00:00
|
|
|
MutableBlobStorage::MutableBlobStorageType mBlobStorageType;
|
|
|
|
nsCString mBodyMimeType;
|
2017-06-20 15:53:21 +00:00
|
|
|
|
2018-10-06 17:34:24 +00:00
|
|
|
nsCString mBodyBlobURISpec;
|
2018-09-11 19:13:15 +00:00
|
|
|
nsString mBodyLocalPath;
|
|
|
|
|
2017-06-22 11:16:32 +00:00
|
|
|
nsCOMPtr<nsIGlobalObject> mGlobal;
|
|
|
|
|
2017-07-10 20:07:44 +00:00
|
|
|
// Touched on the main-thread only.
|
|
|
|
nsCOMPtr<nsIInputStreamPump> mConsumeBodyPump;
|
2017-06-20 15:53:21 +00:00
|
|
|
|
|
|
|
// Only ever set once, always on target thread.
|
|
|
|
FetchConsumeType mConsumeType;
|
|
|
|
RefPtr<Promise> mConsumePromise;
|
|
|
|
|
2017-07-10 20:07:44 +00:00
|
|
|
// touched only on the target thread.
|
2017-06-28 18:28:42 +00:00
|
|
|
bool mBodyConsumed;
|
2017-07-10 20:07:44 +00:00
|
|
|
|
|
|
|
// touched only on the main-thread.
|
|
|
|
bool mShuttingDown;
|
2017-06-20 15:53:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_FetchConsumer_h
|