Bumping manifests a=b2g-bump
[gecko.git] / dom / base / DOMRequest.h
blob56236c1567793a2c886561fc050732429d46cd3e
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=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 "nsIDOMDOMRequest.h"
11 #include "mozilla/Attributes.h"
12 #include "mozilla/DOMEventTargetHelper.h"
13 #include "mozilla/dom/DOMError.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 public nsIDOMDOMRequest
30 protected:
31 JS::Heap<JS::Value> mResult;
32 nsRefPtr<DOMError> mError;
33 nsRefPtr<Promise> mPromise;
34 bool mDone;
36 public:
37 NS_DECL_ISUPPORTS_INHERITED
38 NS_DECL_NSIDOMDOMREQUEST
39 NS_REALLY_FORWARD_NSIDOMEVENTTARGET(DOMEventTargetHelper)
41 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(DOMRequest,
42 DOMEventTargetHelper)
44 // WrapperCache
45 nsPIDOMWindow* GetParentObject() const
47 return GetOwner();
50 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
52 // WebIDL Interface
53 DOMRequestReadyState ReadyState() const
55 return mDone ? DOMRequestReadyState::Done
56 : DOMRequestReadyState::Pending;
59 void GetResult(JSContext*, JS::MutableHandle<JS::Value> aRetval) const
61 NS_ASSERTION(mDone || mResult == JSVAL_VOID,
62 "Result should be undefined when pending");
63 JS::ExposeValueToActiveJS(mResult);
64 aRetval.set(mResult);
67 DOMError* GetError() const
69 NS_ASSERTION(mDone || !mError,
70 "Error should be null when pending");
71 return mError;
74 IMPL_EVENT_HANDLER(success)
75 IMPL_EVENT_HANDLER(error)
77 already_AddRefed<mozilla::dom::Promise>
78 Then(JSContext* aCx, AnyCallback* aResolveCallback,
79 AnyCallback* aRejectCallback, mozilla::ErrorResult& aRv);
81 void FireSuccess(JS::Handle<JS::Value> aResult);
82 void FireError(const nsAString& aError);
83 void FireError(nsresult aError);
84 void FireDetailedError(DOMError* aError);
86 explicit DOMRequest(nsPIDOMWindow* aWindow);
88 protected:
89 virtual ~DOMRequest();
91 void FireEvent(const nsAString& aType, bool aBubble, bool aCancelable);
93 void RootResultVal();
96 class DOMRequestService MOZ_FINAL : public nsIDOMRequestService
98 ~DOMRequestService() {}
100 public:
101 NS_DECL_ISUPPORTS
102 NS_DECL_NSIDOMREQUESTSERVICE
104 // Returns an owning reference! No one should call this but the factory.
105 static DOMRequestService* FactoryCreate()
107 DOMRequestService* res = new DOMRequestService;
108 NS_ADDREF(res);
109 return res;
113 } // namespace dom
114 } // namespace mozilla
116 #define DOMREQUEST_SERVICE_CONTRACTID "@mozilla.org/dom/dom-request-service;1"
118 #endif // mozilla_dom_domrequest_h__