2013-07-11 13:40:36 -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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_PromiseCallback_h
|
|
|
|
#define mozilla_dom_PromiseCallback_h
|
|
|
|
|
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
// This is the base class for any PromiseCallback.
|
|
|
|
// It's a logical step in the promise chain of callbacks.
|
|
|
|
class PromiseCallback : public nsISupports
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS(PromiseCallback)
|
|
|
|
|
|
|
|
PromiseCallback();
|
|
|
|
virtual ~PromiseCallback();
|
|
|
|
|
2013-11-19 10:39:51 -08:00
|
|
|
virtual void Call(JS::Handle<JS::Value> aValue) = 0;
|
2013-07-11 13:40:36 -07:00
|
|
|
|
|
|
|
enum Task {
|
|
|
|
Resolve,
|
|
|
|
Reject
|
|
|
|
};
|
|
|
|
|
|
|
|
// This factory returns a PromiseCallback object with refcount of 0.
|
|
|
|
static PromiseCallback*
|
2013-09-11 09:03:04 -07:00
|
|
|
Factory(Promise* aNextPromise, AnyCallback* aCallback, Task aTask);
|
2013-07-11 13:40:36 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// WrapperPromiseCallback execs a JS Callback with a value, and then the return
|
2013-11-24 11:26:07 -08:00
|
|
|
// value is sent to the aNextPromise->ResolveFunction() or to
|
2013-09-11 09:03:04 -07:00
|
|
|
// aNextPromise->RejectFunction() if the JS Callback throws.
|
2013-07-11 13:40:36 -07:00
|
|
|
class WrapperPromiseCallback MOZ_FINAL : public PromiseCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(WrapperPromiseCallback,
|
|
|
|
PromiseCallback)
|
|
|
|
|
2013-11-19 10:39:51 -08:00
|
|
|
void Call(JS::Handle<JS::Value> aValue) MOZ_OVERRIDE;
|
2013-07-11 13:40:36 -07:00
|
|
|
|
2013-09-11 09:03:04 -07:00
|
|
|
WrapperPromiseCallback(Promise* aNextPromise, AnyCallback* aCallback);
|
2013-07-11 13:40:36 -07:00
|
|
|
~WrapperPromiseCallback();
|
|
|
|
|
|
|
|
private:
|
2013-09-11 09:03:04 -07:00
|
|
|
nsRefPtr<Promise> mNextPromise;
|
2013-07-11 13:40:36 -07:00
|
|
|
nsRefPtr<AnyCallback> mCallback;
|
|
|
|
};
|
|
|
|
|
2013-09-11 09:03:04 -07:00
|
|
|
// ResolvePromiseCallback calls aPromise->ResolveFunction() with the value
|
|
|
|
// received by Call().
|
2013-07-11 13:40:36 -07:00
|
|
|
class ResolvePromiseCallback MOZ_FINAL : public PromiseCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ResolvePromiseCallback,
|
|
|
|
PromiseCallback)
|
|
|
|
|
2013-11-19 10:39:51 -08:00
|
|
|
void Call(JS::Handle<JS::Value> aValue) MOZ_OVERRIDE;
|
2013-07-11 13:40:36 -07:00
|
|
|
|
2013-09-11 09:03:04 -07:00
|
|
|
ResolvePromiseCallback(Promise* aPromise);
|
2013-07-11 13:40:36 -07:00
|
|
|
~ResolvePromiseCallback();
|
|
|
|
|
|
|
|
private:
|
2013-09-11 09:03:04 -07:00
|
|
|
nsRefPtr<Promise> mPromise;
|
2013-07-11 13:40:36 -07:00
|
|
|
};
|
|
|
|
|
2013-09-11 09:03:04 -07:00
|
|
|
// RejectPromiseCallback calls aPromise->RejectFunction() with the value
|
|
|
|
// received by Call().
|
2013-07-11 13:40:36 -07:00
|
|
|
class RejectPromiseCallback MOZ_FINAL : public PromiseCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(RejectPromiseCallback,
|
|
|
|
PromiseCallback)
|
|
|
|
|
2013-11-19 10:39:51 -08:00
|
|
|
void Call(JS::Handle<JS::Value> aValue) MOZ_OVERRIDE;
|
2013-07-11 13:40:36 -07:00
|
|
|
|
2013-09-11 09:03:04 -07:00
|
|
|
RejectPromiseCallback(Promise* aPromise);
|
2013-07-11 13:40:36 -07:00
|
|
|
~RejectPromiseCallback();
|
|
|
|
|
|
|
|
private:
|
2013-09-11 09:03:04 -07:00
|
|
|
nsRefPtr<Promise> mPromise;
|
2013-07-11 13:40:36 -07:00
|
|
|
};
|
|
|
|
|
2013-11-19 10:43:51 -08:00
|
|
|
// NativePromiseCallback wraps a NativePromiseHandler.
|
|
|
|
class NativePromiseCallback MOZ_FINAL : public PromiseCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(NativePromiseCallback,
|
|
|
|
PromiseCallback)
|
|
|
|
|
|
|
|
void Call(JS::Handle<JS::Value> aValue) MOZ_OVERRIDE;
|
|
|
|
|
|
|
|
NativePromiseCallback(PromiseNativeHandler* aHandler,
|
|
|
|
Promise::PromiseState aState);
|
|
|
|
~NativePromiseCallback();
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<PromiseNativeHandler> mHandler;
|
|
|
|
Promise::PromiseState mState;
|
|
|
|
};
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_PromiseCallback_h
|