Bug 1842773 - Part 5: Add ArrayBuffer.prototype.{maxByteLength,resizable} getters...
[gecko.git] / dom / base / DOMRequest.h
blobb0e7c23112c2cbe352b631a12740b2defebe8916
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"
16 #include "nsCOMPtr.h"
18 namespace mozilla {
20 class ErrorResult;
22 namespace dom {
24 class AnyCallback;
25 class Promise;
27 class DOMRequest : public DOMEventTargetHelper {
28 protected:
29 JS::Heap<JS::Value> mResult;
30 RefPtr<DOMException> mError;
31 RefPtr<Promise> mPromise;
32 bool mDone;
34 public:
35 NS_DECL_ISUPPORTS_INHERITED
37 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(DOMRequest,
38 DOMEventTargetHelper)
40 // WrapperCache
41 nsPIDOMWindowInner* GetParentObject() const { return GetOwner(); }
43 virtual JSObject* WrapObject(JSContext* aCx,
44 JS::Handle<JSObject*> aGivenProto) override;
46 // WebIDL Interface
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");
54 aRetval.set(mResult);
57 DOMException* GetError() const {
58 NS_ASSERTION(mDone || !mError, "Error should be null when pending");
59 return mError;
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);
77 protected:
78 virtual ~DOMRequest();
80 void FireEvent(const nsAString& aType, bool aBubble, bool aCancelable);
82 void RootResultVal();
85 class DOMRequestService final : public nsIDOMRequestService {
86 ~DOMRequestService() = default;
88 public:
89 NS_DECL_ISUPPORTS
90 NS_DECL_NSIDOMREQUESTSERVICE
92 // No one should call this but the factory.
93 static already_AddRefed<DOMRequestService> FactoryCreate() {
94 return MakeAndAddRef<DOMRequestService>();
98 } // namespace dom
99 } // namespace mozilla
101 #define DOMREQUEST_SERVICE_CONTRACTID "@mozilla.org/dom/dom-request-service;1"
103 #endif // mozilla_dom_domrequest_h__