2013-06-11 18:41:21 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
#ifndef mozilla_dom_Promise_h
|
|
|
|
#define mozilla_dom_Promise_h
|
2013-06-11 18:41:21 -07:00
|
|
|
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2013-07-11 13:40:36 -07:00
|
|
|
#include "mozilla/dom/PromiseBinding.h"
|
2013-06-11 18:41:21 -07:00
|
|
|
#include "nsWrapperCache.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
|
|
|
|
struct JSContext;
|
2013-08-22 23:40:32 -07:00
|
|
|
class nsPIDOMWindow;
|
2013-06-11 18:41:21 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
class PromiseInit;
|
|
|
|
class PromiseCallback;
|
2013-06-11 18:41:21 -07:00
|
|
|
class AnyCallback;
|
2013-07-11 13:40:36 -07:00
|
|
|
class PromiseResolver;
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
class Promise MOZ_FINAL : public nsISupports,
|
|
|
|
public nsWrapperCache
|
2013-06-11 18:41:21 -07:00
|
|
|
{
|
2013-07-11 13:40:36 -07:00
|
|
|
friend class PromiseTask;
|
|
|
|
friend class PromiseResolver;
|
|
|
|
friend class PromiseResolverTask;
|
2013-06-11 18:41:21 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2013-07-11 13:40:36 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Promise)
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
Promise(nsPIDOMWindow* aWindow);
|
|
|
|
~Promise();
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2013-06-12 06:44:56 -07:00
|
|
|
static bool PrefEnabled();
|
2013-08-07 14:40:20 -07:00
|
|
|
static bool EnabledForScope(JSContext* aCx, JSObject* /* unused */);
|
2013-06-12 06:44:56 -07:00
|
|
|
|
2013-06-11 18:41:21 -07:00
|
|
|
// WebIDL
|
|
|
|
|
|
|
|
nsPIDOMWindow* GetParentObject() const
|
|
|
|
{
|
|
|
|
return mWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual JSObject*
|
|
|
|
WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
static already_AddRefed<Promise>
|
2013-08-22 22:17:08 -07:00
|
|
|
Constructor(const GlobalObject& aGlobal, PromiseInit& aInit,
|
2013-06-11 18:41:21 -07:00
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
static already_AddRefed<Promise>
|
2013-06-11 18:41:22 -07:00
|
|
|
Resolve(const GlobalObject& aGlobal, JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue, ErrorResult& aRv);
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
static already_AddRefed<Promise>
|
2013-06-11 18:41:22 -07:00
|
|
|
Reject(const GlobalObject& aGlobal, JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue, ErrorResult& aRv);
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
already_AddRefed<Promise>
|
2013-07-16 06:56:24 -07:00
|
|
|
Then(const Optional<OwningNonNull<AnyCallback> >& aResolveCallback,
|
|
|
|
const Optional<OwningNonNull<AnyCallback> >& aRejectCallback);
|
2013-06-11 18:41:22 -07:00
|
|
|
|
2013-06-20 03:20:01 -07:00
|
|
|
|
2013-07-16 06:56:24 -07:00
|
|
|
already_AddRefed<Promise>
|
|
|
|
Catch(const Optional<OwningNonNull<AnyCallback> >& aRejectCallback);
|
2013-06-11 18:41:21 -07:00
|
|
|
|
|
|
|
private:
|
2013-07-11 13:40:36 -07:00
|
|
|
enum PromiseState {
|
2013-06-11 18:41:21 -07:00
|
|
|
Pending,
|
|
|
|
Resolved,
|
|
|
|
Rejected
|
|
|
|
};
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
void SetState(PromiseState aState)
|
2013-06-11 18:41:21 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mState == Pending);
|
|
|
|
MOZ_ASSERT(aState != Pending);
|
|
|
|
mState = aState;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetResult(JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
|
|
|
mResult = aValue;
|
|
|
|
}
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
// This method processes promise's resolve/reject callbacks with promise's
|
2013-06-11 18:41:21 -07:00
|
|
|
// result. It's executed when the resolver.resolve() or resolver.reject() is
|
2013-07-11 13:40:36 -07:00
|
|
|
// called or when the promise already has a result and new callbacks are
|
2013-06-11 18:41:21 -07:00
|
|
|
// appended by then(), catch() or done().
|
|
|
|
void RunTask();
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
void AppendCallbacks(PromiseCallback* aResolveCallback,
|
|
|
|
PromiseCallback* aRejectCallback);
|
2013-06-11 18:41:21 -07:00
|
|
|
|
|
|
|
nsRefPtr<nsPIDOMWindow> mWindow;
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
nsRefPtr<PromiseResolver> mResolver;
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
nsTArray<nsRefPtr<PromiseCallback> > mResolveCallbacks;
|
|
|
|
nsTArray<nsRefPtr<PromiseCallback> > mRejectCallbacks;
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
JS::Heap<JS::Value> mResult;
|
2013-07-11 13:40:36 -07:00
|
|
|
PromiseState mState;
|
2013-06-11 18:41:21 -07:00
|
|
|
bool mTaskPending;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
#endif // mozilla_dom_Promise_h
|