michael@0: /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_dom_workers_xmlhttprequest_h__ michael@0: #define mozilla_dom_workers_xmlhttprequest_h__ michael@0: michael@0: #include "mozilla/dom/workers/bindings/WorkerFeature.h" michael@0: michael@0: // Need this for XMLHttpRequestResponseType. michael@0: #include "mozilla/dom/XMLHttpRequestBinding.h" michael@0: michael@0: #include "mozilla/dom/TypedArray.h" michael@0: michael@0: #include "js/StructuredClone.h" michael@0: #include "nsXMLHttpRequest.h" michael@0: michael@0: BEGIN_WORKERS_NAMESPACE michael@0: michael@0: class Proxy; michael@0: class XMLHttpRequestUpload; michael@0: class WorkerPrivate; michael@0: michael@0: class XMLHttpRequest MOZ_FINAL: public nsXHREventTarget, michael@0: public WorkerFeature michael@0: { michael@0: public: michael@0: struct StateData michael@0: { michael@0: nsString mResponseText; michael@0: uint32_t mStatus; michael@0: nsCString mStatusText; michael@0: uint16_t mReadyState; michael@0: JS::Heap mResponse; michael@0: nsresult mResponseTextResult; michael@0: nsresult mStatusResult; michael@0: nsresult mResponseResult; michael@0: michael@0: StateData() michael@0: : mStatus(0), mReadyState(0), mResponse(JSVAL_VOID), michael@0: mResponseTextResult(NS_OK), mStatusResult(NS_OK), michael@0: mResponseResult(NS_OK) michael@0: { } michael@0: }; michael@0: michael@0: private: michael@0: nsRefPtr mUpload; michael@0: WorkerPrivate* mWorkerPrivate; michael@0: nsRefPtr mProxy; michael@0: XMLHttpRequestResponseType mResponseType; michael@0: StateData mStateData; michael@0: michael@0: uint32_t mTimeout; michael@0: michael@0: bool mRooted; michael@0: bool mBackgroundRequest; michael@0: bool mWithCredentials; michael@0: bool mCanceled; michael@0: michael@0: bool mMozAnon; michael@0: bool mMozSystem; michael@0: michael@0: public: michael@0: virtual JSObject* michael@0: WrapObject(JSContext* aCx) MOZ_OVERRIDE; michael@0: michael@0: NS_DECL_ISUPPORTS_INHERITED michael@0: NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(XMLHttpRequest, michael@0: nsXHREventTarget) michael@0: michael@0: nsISupports* michael@0: GetParentObject() const michael@0: { michael@0: // There's only one global on a worker, so we don't need to specify. michael@0: return nullptr; michael@0: } michael@0: michael@0: static already_AddRefed michael@0: Constructor(const GlobalObject& aGlobal, michael@0: const MozXMLHttpRequestParameters& aParams, michael@0: ErrorResult& aRv); michael@0: michael@0: static already_AddRefed michael@0: Constructor(const GlobalObject& aGlobal, const nsAString& ignored, michael@0: ErrorResult& aRv) michael@0: { michael@0: // Pretend like someone passed null, so we can pick up the default values michael@0: MozXMLHttpRequestParameters params; michael@0: if (!params.Init(aGlobal.GetContext(), JS::NullHandleValue)) { michael@0: aRv.Throw(NS_ERROR_UNEXPECTED); michael@0: return nullptr; michael@0: } michael@0: michael@0: return Constructor(aGlobal, params, aRv); michael@0: } michael@0: michael@0: void michael@0: Unpin(); michael@0: michael@0: bool michael@0: Notify(JSContext* aCx, Status aStatus) MOZ_OVERRIDE; michael@0: michael@0: IMPL_EVENT_HANDLER(readystatechange) michael@0: michael@0: uint16_t michael@0: ReadyState() const michael@0: { michael@0: return mStateData.mReadyState; michael@0: } michael@0: michael@0: void Open(const nsACString& aMethod, const nsAString& aUrl, ErrorResult& aRv) michael@0: { michael@0: Open(aMethod, aUrl, true, Optional(), michael@0: Optional(), aRv); michael@0: } michael@0: void michael@0: Open(const nsACString& aMethod, const nsAString& aUrl, bool aAsync, michael@0: const Optional& aUser, const Optional& aPassword, michael@0: ErrorResult& aRv); michael@0: michael@0: void michael@0: SetRequestHeader(const nsACString& aHeader, const nsACString& aValue, michael@0: ErrorResult& aRv); michael@0: michael@0: uint32_t michael@0: Timeout() const michael@0: { michael@0: return mTimeout; michael@0: } michael@0: michael@0: void michael@0: SetTimeout(uint32_t aTimeout, ErrorResult& aRv); michael@0: michael@0: bool michael@0: WithCredentials() const michael@0: { michael@0: return mWithCredentials; michael@0: } michael@0: michael@0: void michael@0: SetWithCredentials(bool aWithCredentials, ErrorResult& aRv); michael@0: michael@0: bool michael@0: MozBackgroundRequest() const michael@0: { michael@0: return mBackgroundRequest; michael@0: } michael@0: michael@0: void michael@0: SetMozBackgroundRequest(bool aBackgroundRequest, ErrorResult& aRv); michael@0: michael@0: XMLHttpRequestUpload* michael@0: GetUpload(ErrorResult& aRv); michael@0: michael@0: void michael@0: Send(ErrorResult& aRv); michael@0: michael@0: void michael@0: Send(const nsAString& aBody, ErrorResult& aRv); michael@0: michael@0: void michael@0: Send(JS::Handle aBody, ErrorResult& aRv); michael@0: michael@0: void michael@0: Send(const ArrayBuffer& aBody, ErrorResult& aRv); michael@0: michael@0: void michael@0: Send(const ArrayBufferView& aBody, ErrorResult& aRv); michael@0: michael@0: void michael@0: SendAsBinary(const nsAString& aBody, ErrorResult& aRv); michael@0: michael@0: void michael@0: Abort(ErrorResult& aRv); michael@0: michael@0: uint16_t michael@0: GetStatus(ErrorResult& aRv) const michael@0: { michael@0: aRv = mStateData.mStatusResult; michael@0: return mStateData.mStatus; michael@0: } michael@0: michael@0: void michael@0: GetStatusText(nsACString& aStatusText) const michael@0: { michael@0: aStatusText = mStateData.mStatusText; michael@0: } michael@0: michael@0: void michael@0: GetResponseHeader(const nsACString& aHeader, nsACString& aResponseHeader, michael@0: ErrorResult& aRv); michael@0: michael@0: void michael@0: GetAllResponseHeaders(nsACString& aResponseHeaders, ErrorResult& aRv); michael@0: michael@0: void michael@0: OverrideMimeType(const nsAString& aMimeType, ErrorResult& aRv); michael@0: michael@0: XMLHttpRequestResponseType michael@0: ResponseType() const michael@0: { michael@0: return mResponseType; michael@0: } michael@0: michael@0: void michael@0: SetResponseType(XMLHttpRequestResponseType aResponseType, ErrorResult& aRv); michael@0: michael@0: void michael@0: GetResponse(JSContext* /* unused */, JS::MutableHandle aResponse, michael@0: ErrorResult& aRv); michael@0: michael@0: void michael@0: GetResponseText(nsAString& aResponseText, ErrorResult& aRv); michael@0: michael@0: JSObject* michael@0: GetResponseXML() const michael@0: { michael@0: return nullptr; michael@0: } michael@0: michael@0: JSObject* michael@0: GetChannel() const michael@0: { michael@0: return nullptr; michael@0: } michael@0: michael@0: void michael@0: GetInterface(JSContext* cx, JS::Handle aIID, michael@0: JS::MutableHandle aRetval, ErrorResult& aRv) michael@0: { michael@0: aRv.Throw(NS_ERROR_FAILURE); michael@0: } michael@0: michael@0: XMLHttpRequestUpload* michael@0: GetUploadObjectNoCreate() const michael@0: { michael@0: return mUpload; michael@0: } michael@0: michael@0: void michael@0: UpdateState(const StateData& aStateData); michael@0: michael@0: void michael@0: NullResponseText() michael@0: { michael@0: mStateData.mResponseText.SetIsVoid(true); michael@0: mStateData.mResponse = JSVAL_NULL; michael@0: } michael@0: michael@0: bool MozAnon() const michael@0: { michael@0: return mMozAnon; michael@0: } michael@0: michael@0: bool MozSystem() const michael@0: { michael@0: return mMozSystem; michael@0: } michael@0: michael@0: private: michael@0: XMLHttpRequest(WorkerPrivate* aWorkerPrivate); michael@0: ~XMLHttpRequest(); michael@0: michael@0: enum ReleaseType { Default, XHRIsGoingAway, WorkerIsGoingAway }; michael@0: michael@0: void michael@0: ReleaseProxy(ReleaseType aType = Default); michael@0: michael@0: void michael@0: MaybePin(ErrorResult& aRv); michael@0: michael@0: void michael@0: MaybeDispatchPrematureAbortEvents(ErrorResult& aRv); michael@0: michael@0: void michael@0: DispatchPrematureAbortEvent(EventTarget* aTarget, michael@0: const nsAString& aEventType, bool aUploadTarget, michael@0: ErrorResult& aRv); michael@0: michael@0: bool michael@0: SendInProgress() const michael@0: { michael@0: return mRooted; michael@0: } michael@0: michael@0: void michael@0: SendInternal(const nsAString& aStringBody, michael@0: JSAutoStructuredCloneBuffer&& aBody, michael@0: nsTArray >& aClonedObjects, michael@0: ErrorResult& aRv); michael@0: }; michael@0: michael@0: END_WORKERS_NAMESPACE michael@0: michael@0: #endif // mozilla_dom_workers_xmlhttprequest_h__