1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_domrequest_h__
8 #define mozilla_dom_domrequest_h__
10 #include "nsIDOMRequestService.h"
11 #include "mozilla/Attributes.h"
12 #include "mozilla/DOMEventTargetHelper.h"
13 #include "mozilla/dom/DOMException.h"
14 #include "mozilla/dom/DOMRequestBinding.h"
27 class DOMRequest
: public DOMEventTargetHelper
{
29 JS::Heap
<JS::Value
> mResult
;
30 RefPtr
<DOMException
> mError
;
31 RefPtr
<Promise
> mPromise
;
35 NS_DECL_ISUPPORTS_INHERITED
37 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(DOMRequest
,
41 nsPIDOMWindowInner
* GetParentObject() const { return GetOwner(); }
43 virtual JSObject
* WrapObject(JSContext
* aCx
,
44 JS::Handle
<JSObject
*> aGivenProto
) override
;
47 DOMRequestReadyState
ReadyState() const {
48 return mDone
? DOMRequestReadyState::Done
: DOMRequestReadyState::Pending
;
51 void GetResult(JSContext
*, JS::MutableHandle
<JS::Value
> aRetval
) const {
52 NS_ASSERTION(mDone
|| mResult
.isUndefined(),
53 "Result should be undefined when pending");
57 DOMException
* GetError() const {
58 NS_ASSERTION(mDone
|| !mError
, "Error should be null when pending");
62 IMPL_EVENT_HANDLER(success
)
63 IMPL_EVENT_HANDLER(error
)
65 void Then(JSContext
* aCx
, AnyCallback
* aResolveCallback
,
66 AnyCallback
* aRejectCallback
, JS::MutableHandle
<JS::Value
> aRetval
,
67 mozilla::ErrorResult
& aRv
);
69 void FireSuccess(JS::Handle
<JS::Value
> aResult
);
70 void FireError(const nsAString
& aError
);
71 void FireError(nsresult aError
);
72 void FireDetailedError(DOMException
& aError
);
74 explicit DOMRequest(nsPIDOMWindowInner
* aWindow
);
75 explicit DOMRequest(nsIGlobalObject
* aGlobal
);
78 virtual ~DOMRequest();
80 void FireEvent(const nsAString
& aType
, bool aBubble
, bool aCancelable
);
85 class DOMRequestService final
: public nsIDOMRequestService
{
86 ~DOMRequestService() = default;
90 NS_DECL_NSIDOMREQUESTSERVICE
92 // No one should call this but the factory.
93 static already_AddRefed
<DOMRequestService
> FactoryCreate() {
94 return MakeAndAddRef
<DOMRequestService
>();
99 } // namespace mozilla
101 #define DOMREQUEST_SERVICE_CONTRACTID "@mozilla.org/dom/dom-request-service;1"
103 #endif // mozilla_dom_domrequest_h__