2015-05-03 12:32:37 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-07-11 13:40:36 -07:00
|
|
|
/* 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();
|
|
|
|
|
2015-04-27 12:00:41 -07:00
|
|
|
virtual nsresult 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
|
2015-11-25 12:48:10 -08:00
|
|
|
// value is sent to either:
|
|
|
|
// a) If aNextPromise is non-null, the aNextPromise->ResolveFunction() or to
|
|
|
|
// aNextPromise->RejectFunction() if the JS Callback throws.
|
|
|
|
// or
|
|
|
|
// b) If aNextPromise is null, in which case aResolveFunc and aRejectFunc must
|
|
|
|
// be non-null, then to aResolveFunc, unless aCallback threw, in which case
|
|
|
|
// aRejectFunc.
|
2015-03-21 09:28:04 -07:00
|
|
|
class WrapperPromiseCallback final : public PromiseCallback
|
2013-07-11 13:40:36 -07:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2015-04-27 12:00:41 -07:00
|
|
|
nsresult Call(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) override;
|
2013-07-11 13:40:36 -07:00
|
|
|
|
2015-11-25 12:48:09 -08:00
|
|
|
Promise* GetDependentPromise() override;
|
2014-10-19 19:27:36 -07:00
|
|
|
|
2015-11-25 12:48:10 -08:00
|
|
|
// Constructor for when we know we have a vanilla Promise.
|
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
|
|
|
|
2015-11-25 12:48:10 -08:00
|
|
|
// Constructor for when all we have to work with are resolve/reject functions.
|
|
|
|
WrapperPromiseCallback(JS::Handle<JSObject*> aGlobal,
|
|
|
|
AnyCallback* aCallback,
|
|
|
|
JS::Handle<JSObject*> mNextPromiseObj,
|
|
|
|
AnyCallback* aResolveFunc,
|
|
|
|
AnyCallback* aRejectFunc);
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
private:
|
2014-07-08 14:23:17 -07:00
|
|
|
~WrapperPromiseCallback();
|
|
|
|
|
2015-11-25 12:48:10 -08:00
|
|
|
// Either mNextPromise is non-null or all three of mNextPromiseObj,
|
|
|
|
// mResolveFund and mRejectFunc must are non-null.
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<Promise> mNextPromise;
|
2015-11-25 12:48:10 -08:00
|
|
|
// mNextPromiseObj is the reflector itself; it may not be in the
|
|
|
|
// same compartment as anything else we have.
|
|
|
|
JS::Heap<JSObject*> mNextPromiseObj;
|
|
|
|
RefPtr<AnyCallback> mResolveFunc;
|
|
|
|
RefPtr<AnyCallback> mRejectFunc;
|
2014-04-09 01:30:24 -07:00
|
|
|
JS::Heap<JSObject*> mGlobal;
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<AnyCallback> mCallback;
|
2013-07-11 13:40:36 -07:00
|
|
|
};
|
|
|
|
|
2013-09-11 09:03:04 -07:00
|
|
|
// ResolvePromiseCallback calls aPromise->ResolveFunction() with the value
|
|
|
|
// received by Call().
|
2015-03-21 09:28:04 -07:00
|
|
|
class ResolvePromiseCallback final : public PromiseCallback
|
2013-07-11 13:40:36 -07:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2015-04-27 12:00:41 -07:00
|
|
|
nsresult Call(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) override;
|
2013-07-11 13:40:36 -07:00
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
Promise* GetDependentPromise() override
|
2014-10-19 19:27:36 -07:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<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().
|
2015-03-21 09:28:04 -07:00
|
|
|
class RejectPromiseCallback final : public PromiseCallback
|
2013-07-11 13:40:36 -07:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2015-04-27 12:00:41 -07:00
|
|
|
nsresult Call(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) override;
|
2013-07-11 13:40:36 -07:00
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
Promise* GetDependentPromise() override
|
2014-10-19 19:27:36 -07:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<Promise> mPromise;
|
2014-04-09 01:30:24 -07:00
|
|
|
JS::Heap<JSObject*> mGlobal;
|
2013-07-11 13:40:36 -07:00
|
|
|
};
|
|
|
|
|
2015-11-25 12:48:10 -08:00
|
|
|
// InvokePromiseFuncCallback calls the given function with the value
|
|
|
|
// received by Call().
|
|
|
|
class InvokePromiseFuncCallback final : public PromiseCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(InvokePromiseFuncCallback,
|
|
|
|
PromiseCallback)
|
|
|
|
|
|
|
|
nsresult Call(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) override;
|
|
|
|
|
|
|
|
Promise* GetDependentPromise() override;
|
|
|
|
|
|
|
|
InvokePromiseFuncCallback(JS::Handle<JSObject*> aGlobal,
|
|
|
|
JS::Handle<JSObject*> aNextPromiseObj,
|
|
|
|
AnyCallback* aPromiseFunc);
|
|
|
|
|
|
|
|
private:
|
|
|
|
~InvokePromiseFuncCallback();
|
|
|
|
|
|
|
|
JS::Heap<JSObject*> mGlobal;
|
|
|
|
JS::Heap<JSObject*> mNextPromiseObj;
|
|
|
|
RefPtr<AnyCallback> mPromiseFunc;
|
|
|
|
};
|
|
|
|
|
2015-03-21 10:43:15 -07:00
|
|
|
// NativePromiseCallback wraps a PromiseNativeHandler.
|
2015-03-21 09:28:04 -07:00
|
|
|
class NativePromiseCallback final : public PromiseCallback
|
2013-11-19 10:43:51 -08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(NativePromiseCallback,
|
|
|
|
PromiseCallback)
|
|
|
|
|
2015-04-27 12:00:41 -07:00
|
|
|
nsresult Call(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) override;
|
2013-11-19 10:43:51 -08:00
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
Promise* GetDependentPromise() override
|
2014-10-19 19:27:36 -07:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<PromiseNativeHandler> mHandler;
|
2013-11-19 10:43:51 -08:00
|
|
|
Promise::PromiseState mState;
|
|
|
|
};
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_PromiseCallback_h
|