2014-09-24 09:03:20 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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_Fetch_h
|
|
|
|
#define mozilla_dom_Fetch_h
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
#include "nsIInputStreamPump.h"
|
|
|
|
#include "nsIStreamLoader.h"
|
|
|
|
|
2014-10-01 22:43:26 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsError.h"
|
2014-12-10 11:51:59 +03:00
|
|
|
#include "nsProxyRelease.h"
|
2014-10-01 22:43:26 +04:00
|
|
|
#include "nsString.h"
|
2014-12-10 11:51:59 +03:00
|
|
|
|
|
|
|
#include "mozilla/DebugOnly.h"
|
2014-10-01 22:43:26 +04:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2015-03-02 16:08:00 +03:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2014-10-06 22:01:20 +04:00
|
|
|
#include "mozilla/dom/RequestBinding.h"
|
2014-12-10 11:51:59 +03:00
|
|
|
#include "mozilla/dom/workers/bindings/WorkerFeature.h"
|
2014-09-24 09:03:20 +04:00
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
class nsIOutputStream;
|
2014-10-06 22:01:20 +04:00
|
|
|
class nsIGlobalObject;
|
2014-09-24 09:03:20 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-11-20 14:58:00 +03:00
|
|
|
class ArrayBufferOrArrayBufferViewOrBlobOrUSVStringOrURLSearchParams;
|
2014-10-06 22:01:20 +04:00
|
|
|
class InternalRequest;
|
2014-11-20 14:58:00 +03:00
|
|
|
class OwningArrayBufferOrArrayBufferViewOrBlobOrUSVStringOrURLSearchParams;
|
|
|
|
class RequestOrUSVString;
|
2014-10-06 22:01:20 +04:00
|
|
|
|
|
|
|
namespace workers {
|
|
|
|
class WorkerPrivate;
|
|
|
|
} // namespace workers
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2014-11-20 14:58:00 +03:00
|
|
|
FetchRequest(nsIGlobalObject* aGlobal, const RequestOrUSVString& aInput,
|
2014-10-06 22:01:20 +04:00
|
|
|
const RequestInit& aInit, ErrorResult& aRv);
|
|
|
|
|
|
|
|
nsresult
|
2014-12-23 18:56:19 +03:00
|
|
|
UpdateRequestReferrer(nsIGlobalObject* aGlobal, InternalRequest* aRequest);
|
2014-09-27 03:41:15 +04:00
|
|
|
|
2014-09-24 09:03:20 +04:00
|
|
|
/*
|
|
|
|
* Creates an nsIInputStream based on the fetch specifications 'extract a byte
|
|
|
|
* stream algorithm' - http://fetch.spec.whatwg.org/#concept-bodyinit-extract.
|
|
|
|
* Stores content type in out param aContentType.
|
|
|
|
*/
|
|
|
|
nsresult
|
2014-11-20 14:58:00 +03:00
|
|
|
ExtractByteStreamFromBody(const OwningArrayBufferOrArrayBufferViewOrBlobOrUSVStringOrURLSearchParams& aBodyInit,
|
2014-09-24 09:03:20 +04:00
|
|
|
nsIInputStream** aStream,
|
|
|
|
nsCString& aContentType);
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
/*
|
|
|
|
* Non-owning version.
|
|
|
|
*/
|
|
|
|
nsresult
|
2014-11-20 14:58:00 +03:00
|
|
|
ExtractByteStreamFromBody(const ArrayBufferOrArrayBufferViewOrBlobOrUSVStringOrURLSearchParams& aBodyInit,
|
2014-09-27 03:41:15 +04:00
|
|
|
nsIInputStream** aStream,
|
|
|
|
nsCString& aContentType);
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
template <class Derived> class FetchBodyFeature;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FetchBody's body consumption uses nsIInputStreamPump to read from the
|
|
|
|
* underlying stream to a block of memory, which is then adopted by
|
|
|
|
* ContinueConsumeBody() and converted to the right type based on the JS
|
|
|
|
* function called.
|
|
|
|
*
|
|
|
|
* Use of the nsIInputStreamPump complicates things on the worker thread.
|
|
|
|
* The solution used here is similar to WebSockets.
|
|
|
|
* The difference is that we are only interested in completion and not data
|
|
|
|
* events, and nsIInputStreamPump can only deliver completion on the main thread.
|
|
|
|
*
|
|
|
|
* Before starting the pump on the main thread, we addref the FetchBody to keep
|
|
|
|
* it alive. Then we add a feature, to track the status of the worker.
|
|
|
|
*
|
|
|
|
* ContinueConsumeBody() is the function that cleans things up in both success
|
|
|
|
* and error conditions and so all callers call it with the appropriate status.
|
|
|
|
*
|
|
|
|
* Once the read is initiated on the main thread there are two possibilities.
|
|
|
|
*
|
|
|
|
* 1) Pump finishes before worker has finished Running.
|
|
|
|
* In this case we adopt the data and dispatch a runnable to the worker,
|
|
|
|
* which derefs FetchBody and removes the feature and resolves the Promise.
|
|
|
|
*
|
|
|
|
* 2) Pump still working while worker has stopped Running.
|
|
|
|
* The feature is Notify()ed and ContinueConsumeBody() is called with
|
|
|
|
* NS_BINDING_ABORTED. We first Cancel() the pump using a sync runnable to
|
|
|
|
* ensure that mFetchBody remains alive (since mConsumeBodyPump is strongly
|
|
|
|
* held by it) until pump->Cancel() is called. OnStreamComplete() will not
|
|
|
|
* do anything if the error code is NS_BINDING_ABORTED, so we don't have to
|
|
|
|
* worry about keeping anything alive.
|
|
|
|
*
|
|
|
|
* The pump is always released on the main thread.
|
|
|
|
*/
|
2014-09-27 03:41:15 +04:00
|
|
|
template <class Derived>
|
|
|
|
class FetchBody {
|
|
|
|
public:
|
|
|
|
bool
|
2015-02-20 04:24:24 +03:00
|
|
|
BodyUsed() const { return mBodyUsed; }
|
2014-09-27 03:41:15 +04:00
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
ArrayBuffer(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
return ConsumeBody(CONSUME_ARRAYBUFFER, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
Blob(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
return ConsumeBody(CONSUME_BLOB, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
Json(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
return ConsumeBody(CONSUME_JSON, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
Text(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
return ConsumeBody(CONSUME_TEXT, aRv);
|
|
|
|
}
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
// Utility public methods accessed by various runnables.
|
|
|
|
void
|
|
|
|
BeginConsumeBodyMainThread();
|
|
|
|
|
|
|
|
void
|
|
|
|
ContinueConsumeBody(nsresult aStatus, uint32_t aLength, uint8_t* aResult);
|
|
|
|
|
|
|
|
void
|
|
|
|
CancelPump();
|
|
|
|
|
2015-02-19 04:34:29 +03:00
|
|
|
void
|
|
|
|
SetBodyUsed()
|
|
|
|
{
|
|
|
|
mBodyUsed = true;
|
|
|
|
}
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
// Always set whenever the FetchBody is created on the worker thread.
|
|
|
|
workers::WorkerPrivate* mWorkerPrivate;
|
|
|
|
|
|
|
|
// Set when consuming the body is attempted on a worker.
|
|
|
|
// Unset when consumption is done/aborted.
|
|
|
|
nsAutoPtr<workers::WorkerFeature> mFeature;
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
protected:
|
2014-12-10 11:51:59 +03:00
|
|
|
FetchBody();
|
|
|
|
|
2015-02-19 04:34:29 +03:00
|
|
|
virtual ~FetchBody();
|
2014-09-27 03:41:15 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
SetMimeType(ErrorResult& aRv);
|
|
|
|
private:
|
|
|
|
enum ConsumeType
|
|
|
|
{
|
|
|
|
CONSUME_ARRAYBUFFER,
|
|
|
|
CONSUME_BLOB,
|
|
|
|
// FormData not supported right now,
|
|
|
|
CONSUME_JSON,
|
|
|
|
CONSUME_TEXT,
|
|
|
|
};
|
|
|
|
|
|
|
|
Derived*
|
|
|
|
DerivedClass() const
|
|
|
|
{
|
|
|
|
return static_cast<Derived*>(const_cast<FetchBody*>(this));
|
|
|
|
}
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
nsresult
|
|
|
|
BeginConsumeBody();
|
|
|
|
|
2014-09-27 03:41:15 +04:00
|
|
|
already_AddRefed<Promise>
|
|
|
|
ConsumeBody(ConsumeType aType, ErrorResult& aRv);
|
|
|
|
|
2014-12-10 11:51:59 +03:00
|
|
|
bool
|
|
|
|
AddRefObject();
|
|
|
|
|
|
|
|
void
|
|
|
|
ReleaseObject();
|
|
|
|
|
|
|
|
bool
|
|
|
|
RegisterFeature();
|
|
|
|
|
|
|
|
void
|
|
|
|
UnregisterFeature();
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsOnTargetThread()
|
|
|
|
{
|
|
|
|
return NS_IsMainThread() == !mWorkerPrivate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AssertIsOnTargetThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsOnTargetThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only ever set once, always on target thread.
|
2014-09-27 03:41:15 +04:00
|
|
|
bool mBodyUsed;
|
|
|
|
nsCString mMimeType;
|
2014-12-10 11:51:59 +03:00
|
|
|
|
|
|
|
// Only touched on target thread.
|
|
|
|
ConsumeType mConsumeType;
|
|
|
|
nsRefPtr<Promise> mConsumePromise;
|
|
|
|
DebugOnly<bool> mReadDone;
|
|
|
|
|
|
|
|
nsMainThreadPtrHandle<nsIInputStreamPump> mConsumeBodyPump;
|
2014-09-27 03:41:15 +04:00
|
|
|
};
|
2014-10-06 22:01:20 +04:00
|
|
|
|
2014-09-24 09:03:20 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_Fetch_h
|