mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-08 20:47:44 +00:00
36db9eff13
--HG-- extra : transplant_source : o%29%0E%5D%90%11%FA%1ExO%26%7E%9D_%F2%E7%0B%FE%21%B2
128 lines
2.9 KiB
C++
128 lines
2.9 KiB
C++
/* -*- 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
|
|
|
|
#include "nsCOMPtr.h"
|
|
#include "nsError.h"
|
|
#include "nsString.h"
|
|
#include "mozilla/ErrorResult.h"
|
|
#include "mozilla/dom/RequestBinding.h"
|
|
|
|
class nsIInputStream;
|
|
class nsIGlobalObject;
|
|
|
|
namespace mozilla {
|
|
namespace dom {
|
|
|
|
class ArrayBufferOrArrayBufferViewOrBlobOrScalarValueStringOrURLSearchParams;
|
|
class InternalRequest;
|
|
class OwningArrayBufferOrArrayBufferViewOrBlobOrScalarValueStringOrURLSearchParams;
|
|
class Promise;
|
|
class RequestOrScalarValueString;
|
|
|
|
namespace workers {
|
|
class WorkerPrivate;
|
|
} // namespace workers
|
|
|
|
already_AddRefed<Promise>
|
|
FetchRequest(nsIGlobalObject* aGlobal, const RequestOrScalarValueString& aInput,
|
|
const RequestInit& aInit, ErrorResult& aRv);
|
|
|
|
nsresult
|
|
GetRequestReferrer(nsIGlobalObject* aGlobal, const InternalRequest* aRequest, nsCString& aReferrer);
|
|
|
|
/*
|
|
* 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
|
|
ExtractByteStreamFromBody(const OwningArrayBufferOrArrayBufferViewOrBlobOrScalarValueStringOrURLSearchParams& aBodyInit,
|
|
nsIInputStream** aStream,
|
|
nsCString& aContentType);
|
|
|
|
/*
|
|
* Non-owning version.
|
|
*/
|
|
nsresult
|
|
ExtractByteStreamFromBody(const ArrayBufferOrArrayBufferViewOrBlobOrScalarValueStringOrURLSearchParams& aBodyInit,
|
|
nsIInputStream** aStream,
|
|
nsCString& aContentType);
|
|
|
|
template <class Derived>
|
|
class FetchBody {
|
|
public:
|
|
bool
|
|
BodyUsed() { return mBodyUsed; }
|
|
|
|
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);
|
|
}
|
|
|
|
protected:
|
|
FetchBody()
|
|
: mBodyUsed(false)
|
|
{
|
|
}
|
|
|
|
void
|
|
SetBodyUsed()
|
|
{
|
|
mBodyUsed = true;
|
|
}
|
|
|
|
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));
|
|
}
|
|
|
|
already_AddRefed<Promise>
|
|
ConsumeBody(ConsumeType aType, ErrorResult& aRv);
|
|
|
|
bool mBodyUsed;
|
|
nsCString mMimeType;
|
|
};
|
|
|
|
} // namespace dom
|
|
} // namespace mozilla
|
|
|
|
#endif // mozilla_dom_Fetch_h
|