2013-07-12 00:40:36 +04: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/. */
|
|
|
|
|
|
|
|
#include "PromiseCallback.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
2013-11-19 22:43:51 +04:00
|
|
|
#include "mozilla/dom/PromiseNativeHandler.h"
|
2013-07-12 00:40:36 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(PromiseCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(PromiseCallback)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PromiseCallback)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(PromiseCallback)
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(PromiseCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(PromiseCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
PromiseCallback::PromiseCallback()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(PromiseCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
PromiseCallback::~PromiseCallback()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(PromiseCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
EnterCompartment(Maybe<JSAutoCompartment>& aAc, JSContext* aCx,
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Handle<JS::Value> aValue)
|
2013-07-12 00:40:36 +04:00
|
|
|
{
|
|
|
|
// FIXME Bug 878849
|
2013-11-19 22:39:51 +04:00
|
|
|
if (aValue.isObject()) {
|
|
|
|
JS::Rooted<JSObject*> rooted(aCx, &aValue.toObject());
|
2013-07-12 00:40:36 +04:00
|
|
|
aAc.construct(aCx, rooted);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolvePromiseCallback
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_1(ResolvePromiseCallback,
|
|
|
|
PromiseCallback,
|
2013-09-11 20:03:04 +04:00
|
|
|
mPromise)
|
2013-07-12 00:40:36 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ResolvePromiseCallback)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(PromiseCallback)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(ResolvePromiseCallback, PromiseCallback)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ResolvePromiseCallback, PromiseCallback)
|
|
|
|
|
2013-09-11 20:03:04 +04:00
|
|
|
ResolvePromiseCallback::ResolvePromiseCallback(Promise* aPromise)
|
|
|
|
: mPromise(aPromise)
|
2013-07-12 00:40:36 +04:00
|
|
|
{
|
2013-09-11 20:03:04 +04:00
|
|
|
MOZ_ASSERT(aPromise);
|
2013-07-12 00:40:36 +04:00
|
|
|
MOZ_COUNT_CTOR(ResolvePromiseCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
ResolvePromiseCallback::~ResolvePromiseCallback()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(ResolvePromiseCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-19 22:39:51 +04:00
|
|
|
ResolvePromiseCallback::Call(JS::Handle<JS::Value> aValue)
|
2013-07-12 00:40:36 +04:00
|
|
|
{
|
|
|
|
// Run resolver's algorithm with value and the synchronous flag set.
|
|
|
|
AutoJSContext cx;
|
|
|
|
Maybe<JSAutoCompartment> ac;
|
|
|
|
EnterCompartment(ac, cx, aValue);
|
|
|
|
|
2013-09-11 20:03:04 +04:00
|
|
|
mPromise->ResolveInternal(cx, aValue, Promise::SyncTask);
|
2013-07-12 00:40:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// RejectPromiseCallback
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_1(RejectPromiseCallback,
|
|
|
|
PromiseCallback,
|
2013-09-11 20:03:04 +04:00
|
|
|
mPromise)
|
2013-07-12 00:40:36 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(RejectPromiseCallback)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(PromiseCallback)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(RejectPromiseCallback, PromiseCallback)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(RejectPromiseCallback, PromiseCallback)
|
|
|
|
|
2013-09-11 20:03:04 +04:00
|
|
|
RejectPromiseCallback::RejectPromiseCallback(Promise* aPromise)
|
|
|
|
: mPromise(aPromise)
|
2013-07-12 00:40:36 +04:00
|
|
|
{
|
2013-09-11 20:03:04 +04:00
|
|
|
MOZ_ASSERT(aPromise);
|
2013-07-12 00:40:36 +04:00
|
|
|
MOZ_COUNT_CTOR(RejectPromiseCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
RejectPromiseCallback::~RejectPromiseCallback()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(RejectPromiseCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-19 22:39:51 +04:00
|
|
|
RejectPromiseCallback::Call(JS::Handle<JS::Value> aValue)
|
2013-07-12 00:40:36 +04:00
|
|
|
{
|
|
|
|
// Run resolver's algorithm with value and the synchronous flag set.
|
|
|
|
AutoJSContext cx;
|
|
|
|
Maybe<JSAutoCompartment> ac;
|
|
|
|
EnterCompartment(ac, cx, aValue);
|
|
|
|
|
2013-09-11 20:03:04 +04:00
|
|
|
mPromise->RejectInternal(cx, aValue, Promise::SyncTask);
|
2013-07-12 00:40:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// WrapperPromiseCallback
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_2(WrapperPromiseCallback,
|
|
|
|
PromiseCallback,
|
2013-09-11 20:03:04 +04:00
|
|
|
mNextPromise, mCallback)
|
2013-07-12 00:40:36 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(WrapperPromiseCallback)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(PromiseCallback)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(WrapperPromiseCallback, PromiseCallback)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(WrapperPromiseCallback, PromiseCallback)
|
|
|
|
|
2013-09-11 20:03:04 +04:00
|
|
|
WrapperPromiseCallback::WrapperPromiseCallback(Promise* aNextPromise,
|
2013-07-12 00:40:36 +04:00
|
|
|
AnyCallback* aCallback)
|
2013-09-11 20:03:04 +04:00
|
|
|
: mNextPromise(aNextPromise)
|
2013-07-12 00:40:36 +04:00
|
|
|
, mCallback(aCallback)
|
|
|
|
{
|
2013-09-11 20:03:04 +04:00
|
|
|
MOZ_ASSERT(aNextPromise);
|
2013-07-12 00:40:36 +04:00
|
|
|
MOZ_COUNT_CTOR(WrapperPromiseCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
WrapperPromiseCallback::~WrapperPromiseCallback()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(WrapperPromiseCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-19 22:39:51 +04:00
|
|
|
WrapperPromiseCallback::Call(JS::Handle<JS::Value> aValue)
|
2013-07-12 00:40:36 +04:00
|
|
|
{
|
|
|
|
AutoJSContext cx;
|
|
|
|
Maybe<JSAutoCompartment> ac;
|
|
|
|
EnterCompartment(ac, cx, aValue);
|
|
|
|
|
|
|
|
ErrorResult rv;
|
|
|
|
|
|
|
|
// If invoking callback threw an exception, run resolver's reject with the
|
|
|
|
// thrown exception as argument and the synchronous flag set.
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Rooted<JS::Value> value(cx,
|
2013-09-11 20:03:04 +04:00
|
|
|
mCallback->Call(mNextPromise->GetParentObject(), aValue, rv,
|
2013-07-12 00:40:36 +04:00
|
|
|
CallbackObject::eRethrowExceptions));
|
|
|
|
|
|
|
|
rv.WouldReportJSException();
|
|
|
|
|
|
|
|
if (rv.Failed() && rv.IsJSException()) {
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Rooted<JS::Value> value(cx);
|
|
|
|
rv.StealJSException(cx, &value);
|
2013-07-12 00:40:36 +04:00
|
|
|
|
|
|
|
Maybe<JSAutoCompartment> ac2;
|
|
|
|
EnterCompartment(ac2, cx, value);
|
2013-09-11 20:03:04 +04:00
|
|
|
mNextPromise->RejectInternal(cx, value, Promise::SyncTask);
|
2013-07-12 00:40:36 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, run resolver's resolve with value and the synchronous flag
|
|
|
|
// set.
|
|
|
|
Maybe<JSAutoCompartment> ac2;
|
|
|
|
EnterCompartment(ac2, cx, value);
|
2013-09-11 20:03:04 +04:00
|
|
|
mNextPromise->ResolveInternal(cx, value, Promise::SyncTask);
|
2013-07-12 00:40:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SimpleWrapperPromiseCallback
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_2(SimpleWrapperPromiseCallback,
|
|
|
|
PromiseCallback,
|
|
|
|
mPromise, mCallback)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(SimpleWrapperPromiseCallback)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(PromiseCallback)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(SimpleWrapperPromiseCallback, PromiseCallback)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(SimpleWrapperPromiseCallback, PromiseCallback)
|
|
|
|
|
|
|
|
SimpleWrapperPromiseCallback::SimpleWrapperPromiseCallback(Promise* aPromise,
|
|
|
|
AnyCallback* aCallback)
|
|
|
|
: mPromise(aPromise)
|
|
|
|
, mCallback(aCallback)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aPromise);
|
|
|
|
MOZ_COUNT_CTOR(SimpleWrapperPromiseCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleWrapperPromiseCallback::~SimpleWrapperPromiseCallback()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(SimpleWrapperPromiseCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-19 22:39:51 +04:00
|
|
|
SimpleWrapperPromiseCallback::Call(JS::Handle<JS::Value> aValue)
|
2013-07-12 00:40:36 +04:00
|
|
|
{
|
|
|
|
ErrorResult rv;
|
|
|
|
mCallback->Call(mPromise, aValue, rv);
|
|
|
|
}
|
|
|
|
|
2013-11-19 22:43:51 +04:00
|
|
|
// NativePromiseCallback
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_1(NativePromiseCallback,
|
|
|
|
PromiseCallback, mHandler)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(NativePromiseCallback)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(PromiseCallback)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(NativePromiseCallback, PromiseCallback)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(NativePromiseCallback, PromiseCallback)
|
|
|
|
|
|
|
|
NativePromiseCallback::NativePromiseCallback(PromiseNativeHandler* aHandler,
|
|
|
|
Promise::PromiseState aState)
|
|
|
|
: mHandler(aHandler)
|
|
|
|
, mState(aState)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aHandler);
|
|
|
|
MOZ_COUNT_CTOR(NativePromiseCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
NativePromiseCallback::~NativePromiseCallback()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(NativePromiseCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NativePromiseCallback::Call(JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
|
|
|
if (mState == Promise::Resolved) {
|
|
|
|
mHandler->ResolvedCallback(aValue);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == Promise::Rejected) {
|
|
|
|
mHandler->RejectedCallback(aValue);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NOTREACHED("huh?");
|
|
|
|
}
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
/* static */ PromiseCallback*
|
2013-09-11 20:03:04 +04:00
|
|
|
PromiseCallback::Factory(Promise* aNextPromise, AnyCallback* aCallback,
|
|
|
|
Task aTask)
|
2013-07-12 00:40:36 +04:00
|
|
|
{
|
2013-09-11 20:03:04 +04:00
|
|
|
MOZ_ASSERT(aNextPromise);
|
2013-07-12 00:40:36 +04:00
|
|
|
|
|
|
|
// If we have a callback and a next resolver, we have to exec the callback and
|
|
|
|
// then propagate the return value to the next resolver->resolve().
|
|
|
|
if (aCallback) {
|
2013-09-11 20:03:04 +04:00
|
|
|
return new WrapperPromiseCallback(aNextPromise, aCallback);
|
2013-07-12 00:40:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aTask == Resolve) {
|
2013-09-11 20:03:04 +04:00
|
|
|
return new ResolvePromiseCallback(aNextPromise);
|
2013-07-12 00:40:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aTask == Reject) {
|
2013-09-11 20:03:04 +04:00
|
|
|
return new RejectPromiseCallback(aNextPromise);
|
2013-07-12 00:40:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(false, "This should not happen");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|