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/. */ 6 7 #ifndef mozilla_dom_domrequest_h__ 8 #define mozilla_dom_domrequest_h__ 9 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" 15 16 #include "nsCOMPtr.h" 17 18 namespace mozilla { 19 20 class ErrorResult; 21 22 namespace dom { 23 24 class AnyCallback; 25 class Promise; 26 27 class DOMRequest : public DOMEventTargetHelper { 28 protected: 29 JS::Heap<JS::Value> mResult; 30 RefPtr<DOMException> mError; 31 RefPtr<Promise> mPromise; 32 bool mDone; 33 34 public: 35 NS_DECL_ISUPPORTS_INHERITED 36 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(DOMRequest,DOMEventTargetHelper)37 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(DOMRequest, 38 DOMEventTargetHelper) 39 40 // WrapperCache 41 nsPIDOMWindowInner* GetParentObject() const { return GetOwner(); } 42 43 virtual JSObject* WrapObject(JSContext* aCx, 44 JS::Handle<JSObject*> aGivenProto) override; 45 46 // WebIDL Interface ReadyState()47 DOMRequestReadyState ReadyState() const { 48 return mDone ? DOMRequestReadyState::Done : DOMRequestReadyState::Pending; 49 } 50 GetResult(JSContext *,JS::MutableHandle<JS::Value> aRetval)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); 55 } 56 GetError()57 DOMException* GetError() const { 58 NS_ASSERTION(mDone || !mError, "Error should be null when pending"); 59 return mError; 60 } 61 62 IMPL_EVENT_HANDLER(success) 63 IMPL_EVENT_HANDLER(error) 64 65 void Then(JSContext* aCx, AnyCallback* aResolveCallback, 66 AnyCallback* aRejectCallback, JS::MutableHandle<JS::Value> aRetval, 67 mozilla::ErrorResult& aRv); 68 69 void FireSuccess(JS::Handle<JS::Value> aResult); 70 void FireError(const nsAString& aError); 71 void FireError(nsresult aError); 72 void FireDetailedError(DOMException& aError); 73 74 explicit DOMRequest(nsPIDOMWindowInner* aWindow); 75 explicit DOMRequest(nsIGlobalObject* aGlobal); 76 77 protected: 78 virtual ~DOMRequest(); 79 80 void FireEvent(const nsAString& aType, bool aBubble, bool aCancelable); 81 82 void RootResultVal(); 83 }; 84 85 class DOMRequestService final : public nsIDOMRequestService { 86 ~DOMRequestService() = default; 87 88 public: 89 NS_DECL_ISUPPORTS 90 NS_DECL_NSIDOMREQUESTSERVICE 91 92 // No one should call this but the factory. FactoryCreate()93 static already_AddRefed<DOMRequestService> FactoryCreate() { 94 return MakeAndAddRef<DOMRequestService>(); 95 } 96 }; 97 98 } // namespace dom 99 } // namespace mozilla 100 101 #define DOMREQUEST_SERVICE_CONTRACTID "@mozilla.org/dom/dom-request-service;1" 102 103 #endif // mozilla_dom_domrequest_h__ 104