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
|
|
|
|
{
|
2014-06-23 12:56:07 -07:00
|
|
|
protected:
|
|
|
|
virtual ~PromiseCallback();
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS(PromiseCallback)
|
|
|
|
|
|
|
|
PromiseCallback();
|
|
|
|
|
2014-04-29 14:12:14 -07:00
|
|
|
virtual void Call(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) = 0;
|
2013-07-11 13:40:36 -07:00
|
|
|
|
2014-10-19 19:27:36 -07:00
|
|
|
// Return the Promise that this callback will end up resolving or
|
|
|
|
// rejecting, if any.
|
|
|
|
virtual Promise* GetDependentPromise() = 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*
|
2014-04-09 01:30:24 -07:00
|
|
|
Factory(Promise* aNextPromise, JS::Handle<JSObject*> aObject,
|
|
|
|
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
|
2014-04-09 01:30:24 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(WrapperPromiseCallback,
|
|
|
|
PromiseCallback)
|
2013-07-11 13:40:36 -07:00
|
|
|
|
2014-04-29 14:12:14 -07:00
|
|
|
void Call(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) MOZ_OVERRIDE;
|
2013-07-11 13:40:36 -07:00
|
|
|
|
2014-10-19 19:27:36 -07:00
|
|
|
Promise* GetDependentPromise() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return mNextPromise;
|
|
|
|
}
|
|
|
|
|
2014-04-09 01:30:24 -07:00
|
|
|
WrapperPromiseCallback(Promise* aNextPromise, JS::Handle<JSObject*> aGlobal,
|
|
|
|
AnyCallback* aCallback);
|
2013-07-11 13:40:36 -07:00
|
|
|
|
|
|
|
private:
|
2014-07-08 14:23:17 -07:00
|
|
|
~WrapperPromiseCallback();
|
|
|
|
|
2013-09-11 09:03:04 -07:00
|
|
|
nsRefPtr<Promise> mNextPromise;
|
2014-04-09 01:30:24 -07:00
|
|
|
JS::Heap<JSObject*> mGlobal;
|
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
|
2014-04-09 01:30:24 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(ResolvePromiseCallback,
|
|
|
|
PromiseCallback)
|
2013-07-11 13:40:36 -07:00
|
|
|
|
2014-04-29 14:12:14 -07:00
|
|
|
void Call(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) MOZ_OVERRIDE;
|
2013-07-11 13:40:36 -07:00
|
|
|
|
2014-10-19 19:27:36 -07:00
|
|
|
Promise* GetDependentPromise() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return mPromise;
|
|
|
|
}
|
|
|
|
|
2014-04-09 01:30:24 -07:00
|
|
|
ResolvePromiseCallback(Promise* aPromise, JS::Handle<JSObject*> aGlobal);
|
2013-07-11 13:40:36 -07:00
|
|
|
|
|
|
|
private:
|
2014-07-08 14:23:17 -07:00
|
|
|
~ResolvePromiseCallback();
|
|
|
|
|
2013-09-11 09:03:04 -07:00
|
|
|
nsRefPtr<Promise> mPromise;
|
2014-04-09 01:30:24 -07:00
|
|
|
JS::Heap<JSObject*> mGlobal;
|
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
|
2014-04-09 01:30:24 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(RejectPromiseCallback,
|
|
|
|
PromiseCallback)
|
2013-07-11 13:40:36 -07:00
|
|
|
|
2014-04-29 14:12:14 -07:00
|
|
|
void Call(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) MOZ_OVERRIDE;
|
2013-07-11 13:40:36 -07:00
|
|
|
|
2014-10-19 19:27:36 -07:00
|
|
|
Promise* GetDependentPromise() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return mPromise;
|
|
|
|
}
|
|
|
|
|
2014-04-09 01:30:24 -07:00
|
|
|
RejectPromiseCallback(Promise* aPromise, JS::Handle<JSObject*> aGlobal);
|
2013-07-11 13:40:36 -07:00
|
|
|
|
|
|
|
private:
|
2014-07-08 14:23:17 -07:00
|
|
|
~RejectPromiseCallback();
|
|
|
|
|
2013-09-11 09:03:04 -07:00
|
|
|
nsRefPtr<Promise> mPromise;
|
2014-04-09 01:30:24 -07:00
|
|
|
JS::Heap<JSObject*> mGlobal;
|
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)
|
|
|
|
|
2014-04-29 14:12:14 -07:00
|
|
|
void Call(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) MOZ_OVERRIDE;
|
2013-11-19 10:43:51 -08:00
|
|
|
|
2014-10-19 19:27:36 -07:00
|
|
|
Promise* GetDependentPromise() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-11-19 10:43:51 -08:00
|
|
|
NativePromiseCallback(PromiseNativeHandler* aHandler,
|
|
|
|
Promise::PromiseState aState);
|
|
|
|
|
|
|
|
private:
|
2014-07-08 14:23:17 -07:00
|
|
|
~NativePromiseCallback();
|
|
|
|
|
2013-11-19 10:43:51 -08:00
|
|
|
nsRefPtr<PromiseNativeHandler> mHandler;
|
|
|
|
Promise::PromiseState mState;
|
|
|
|
};
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_PromiseCallback_h
|