2013-06-12 05:41:21 +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/. */
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2013-08-29 08:30:06 +04:00
|
|
|
|
|
|
|
#include "jsfriendapi.h"
|
2013-09-10 11:03:37 +04:00
|
|
|
#include "mozilla/dom/OwningNonNull.h"
|
2013-07-12 00:40:36 +04:00
|
|
|
#include "mozilla/dom/PromiseBinding.h"
|
2013-06-12 17:44:56 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-07-12 00:40:36 +04:00
|
|
|
#include "PromiseCallback.h"
|
2013-11-19 22:43:51 +04:00
|
|
|
#include "PromiseNativeHandler.h"
|
2013-06-12 05:41:21 +04:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2013-08-08 01:40:20 +04:00
|
|
|
#include "WorkerPrivate.h"
|
2013-10-23 17:16:49 +04:00
|
|
|
#include "WorkerRunnable.h"
|
2013-08-08 01:40:20 +04:00
|
|
|
#include "nsJSPrincipals.h"
|
2013-08-29 08:30:06 +04:00
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsJSEnvironment.h"
|
2013-06-12 05:41:21 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-11-24 23:26:07 +04:00
|
|
|
using namespace workers;
|
|
|
|
|
2013-11-19 22:43:51 +04:00
|
|
|
NS_IMPL_ISUPPORTS0(PromiseNativeHandler)
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
// PromiseTask
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
// This class processes the promise's callbacks with promise's result.
|
|
|
|
class PromiseTask MOZ_FINAL : public nsRunnable
|
2013-06-12 05:41:21 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-07-12 00:40:36 +04:00
|
|
|
PromiseTask(Promise* aPromise)
|
|
|
|
: mPromise(aPromise)
|
2013-06-12 05:41:21 +04:00
|
|
|
{
|
2013-07-12 00:40:36 +04:00
|
|
|
MOZ_ASSERT(aPromise);
|
|
|
|
MOZ_COUNT_CTOR(PromiseTask);
|
2013-06-12 05:41:21 +04:00
|
|
|
}
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
~PromiseTask()
|
2013-06-12 05:41:21 +04:00
|
|
|
{
|
2013-07-12 00:40:36 +04:00
|
|
|
MOZ_COUNT_DTOR(PromiseTask);
|
2013-06-12 05:41:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2013-07-12 00:40:36 +04:00
|
|
|
mPromise->mTaskPending = false;
|
|
|
|
mPromise->RunTask();
|
2013-06-12 05:41:21 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-07-12 00:40:36 +04:00
|
|
|
nsRefPtr<Promise> mPromise;
|
2013-06-12 05:41:21 +04:00
|
|
|
};
|
|
|
|
|
2013-11-24 23:26:07 +04:00
|
|
|
class WorkerPromiseTask MOZ_FINAL : public WorkerRunnable
|
2013-09-11 20:03:04 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-11-24 23:26:07 +04:00
|
|
|
WorkerPromiseTask(WorkerPrivate* aWorkerPrivate, Promise* aPromise)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount)
|
2013-11-24 23:26:07 +04:00
|
|
|
, mPromise(aPromise)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aPromise);
|
|
|
|
MOZ_COUNT_CTOR(WorkerPromiseTask);
|
|
|
|
}
|
|
|
|
|
|
|
|
~WorkerPromiseTask()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(WorkerPromiseTask);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
mPromise->mTaskPending = false;
|
|
|
|
mPromise->RunTask();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<Promise> mPromise;
|
|
|
|
};
|
|
|
|
|
|
|
|
class PromiseResolverMixin
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PromiseResolverMixin(Promise* aPromise,
|
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
Promise::PromiseState aState)
|
2013-09-11 20:03:04 +04:00
|
|
|
: mPromise(aPromise)
|
|
|
|
, mValue(aValue)
|
|
|
|
, mState(aState)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aPromise);
|
|
|
|
MOZ_ASSERT(mState != Promise::Pending);
|
2013-11-24 23:26:07 +04:00
|
|
|
MOZ_COUNT_CTOR(PromiseResolverMixin);
|
2013-09-11 20:03:04 +04:00
|
|
|
|
2013-11-24 23:26:07 +04:00
|
|
|
JSContext* cx = nsContentUtils::GetDefaultJSContextForThread();
|
2013-09-11 20:03:04 +04:00
|
|
|
|
|
|
|
/* It's safe to use unsafeGet() here: the unsafeness comes from the
|
|
|
|
* possibility of updating the value of mJSObject without triggering the
|
|
|
|
* barriers. However if the value will always be marked, post barriers
|
|
|
|
* unnecessary. */
|
|
|
|
JS_AddNamedValueRootRT(JS_GetRuntime(cx), mValue.unsafeGet(),
|
2013-11-24 23:26:07 +04:00
|
|
|
"PromiseResolverMixin.mValue");
|
2013-09-11 20:03:04 +04:00
|
|
|
}
|
|
|
|
|
2013-11-24 23:26:07 +04:00
|
|
|
virtual ~PromiseResolverMixin()
|
2013-09-11 20:03:04 +04:00
|
|
|
{
|
2013-11-24 23:26:07 +04:00
|
|
|
NS_ASSERT_OWNINGTHREAD(PromiseResolverMixin);
|
|
|
|
MOZ_COUNT_DTOR(PromiseResolverMixin);
|
2013-09-11 20:03:04 +04:00
|
|
|
|
2013-11-24 23:26:07 +04:00
|
|
|
JSContext* cx = nsContentUtils::GetDefaultJSContextForThread();
|
2013-09-11 20:03:04 +04:00
|
|
|
|
|
|
|
/* It's safe to use unsafeGet() here: the unsafeness comes from the
|
|
|
|
* possibility of updating the value of mJSObject without triggering the
|
|
|
|
* barriers. However if the value will always be marked, post barriers
|
|
|
|
* unnecessary. */
|
|
|
|
JS_RemoveValueRootRT(JS_GetRuntime(cx), mValue.unsafeGet());
|
|
|
|
}
|
|
|
|
|
2013-11-24 23:26:07 +04:00
|
|
|
protected:
|
|
|
|
void
|
|
|
|
RunInternal()
|
2013-09-11 20:03:04 +04:00
|
|
|
{
|
2013-11-24 23:26:07 +04:00
|
|
|
NS_ASSERT_OWNINGTHREAD(PromiseResolverMixin);
|
2013-09-11 20:03:04 +04:00
|
|
|
mPromise->RunResolveTask(
|
|
|
|
JS::Handle<JS::Value>::fromMarkedLocation(mValue.address()),
|
|
|
|
mState, Promise::SyncTask);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<Promise> mPromise;
|
|
|
|
JS::Heap<JS::Value> mValue;
|
|
|
|
Promise::PromiseState mState;
|
2013-11-24 23:26:07 +04:00
|
|
|
NS_DECL_OWNINGTHREAD;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This class processes the promise's callbacks with promise's result.
|
|
|
|
class PromiseResolverTask MOZ_FINAL : public nsRunnable,
|
|
|
|
public PromiseResolverMixin
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PromiseResolverTask(Promise* aPromise,
|
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
Promise::PromiseState aState)
|
|
|
|
: PromiseResolverMixin(aPromise, aValue, aState)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~PromiseResolverTask()
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
RunInternal();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class WorkerPromiseResolverTask MOZ_FINAL : public WorkerRunnable,
|
|
|
|
public PromiseResolverMixin
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WorkerPromiseResolverTask(WorkerPrivate* aWorkerPrivate,
|
|
|
|
Promise* aPromise,
|
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
Promise::PromiseState aState)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
2013-11-24 23:26:07 +04:00
|
|
|
PromiseResolverMixin(aPromise, aValue, aState)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~WorkerPromiseResolverTask()
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
RunInternal();
|
|
|
|
return true;
|
|
|
|
}
|
2013-09-11 20:03:04 +04:00
|
|
|
};
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
// Promise
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(Promise)
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Promise)
|
2013-08-29 08:30:06 +04:00
|
|
|
tmp->MaybeReportRejected();
|
2013-06-12 05:41:21 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mResolveCallbacks);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mRejectCallbacks);
|
2013-08-29 08:30:06 +04:00
|
|
|
tmp->mResult = JS::UndefinedValue();
|
2013-06-12 05:41:21 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Promise)
|
2013-06-12 05:41:21 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mResolveCallbacks);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRejectCallbacks);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(Promise)
|
2013-06-12 05:41:21 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JSVAL_MEMBER_CALLBACK(mResult)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Promise)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Promise)
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Promise)
|
2013-06-12 05:41:21 +04:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
Promise::Promise(nsPIDOMWindow* aWindow)
|
2013-06-12 05:41:21 +04:00
|
|
|
: mWindow(aWindow)
|
|
|
|
, mResult(JS::UndefinedValue())
|
|
|
|
, mState(Pending)
|
|
|
|
, mTaskPending(false)
|
2013-08-29 08:30:06 +04:00
|
|
|
, mHadRejectCallback(false)
|
2013-09-11 20:03:04 +04:00
|
|
|
, mResolvePending(false)
|
2013-06-12 05:41:21 +04:00
|
|
|
{
|
2013-07-12 00:40:36 +04:00
|
|
|
MOZ_COUNT_CTOR(Promise);
|
2013-08-17 00:10:17 +04:00
|
|
|
mozilla::HoldJSObjects(this);
|
2013-06-12 05:41:21 +04:00
|
|
|
SetIsDOMBinding();
|
|
|
|
}
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
Promise::~Promise()
|
2013-06-12 05:41:21 +04:00
|
|
|
{
|
2013-08-29 08:30:06 +04:00
|
|
|
MaybeReportRejected();
|
|
|
|
mResult = JS::UndefinedValue();
|
2013-08-17 00:10:17 +04:00
|
|
|
mozilla::DropJSObjects(this);
|
2013-07-12 00:40:36 +04:00
|
|
|
MOZ_COUNT_DTOR(Promise);
|
2013-06-12 05:41:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2013-07-12 00:40:36 +04:00
|
|
|
Promise::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
2013-06-12 05:41:21 +04:00
|
|
|
{
|
2013-07-12 00:40:36 +04:00
|
|
|
return PromiseBinding::Wrap(aCx, aScope, this);
|
2013-06-12 05:41:21 +04:00
|
|
|
}
|
|
|
|
|
2013-08-08 01:40:20 +04:00
|
|
|
/* static */ bool
|
|
|
|
Promise::EnabledForScope(JSContext* aCx, JSObject* /* unused */)
|
|
|
|
{
|
2013-11-24 23:26:07 +04:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
// No direct return so the chrome/certified app checks happen below.
|
|
|
|
if (Preferences::GetBool("dom.promise.enabled", false)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
2013-11-24 23:27:15 +04:00
|
|
|
WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx);
|
|
|
|
return workerPrivate->PromiseEnabled() || workerPrivate->UsesSystemPrincipal();
|
2013-11-24 23:26:07 +04:00
|
|
|
}
|
2013-08-08 01:40:20 +04:00
|
|
|
// Enable if the pref is enabled or if we're chrome or if we're a
|
|
|
|
// certified app.
|
|
|
|
// Note that we have no concept of a certified app in workers.
|
|
|
|
// XXXbz well, why not?
|
2013-11-24 23:26:07 +04:00
|
|
|
// FIXME(nsm): Remove these checks once promises are enabled by default.
|
2013-08-08 01:40:20 +04:00
|
|
|
nsIPrincipal* prin = nsContentUtils::GetSubjectPrincipal();
|
|
|
|
return nsContentUtils::IsSystemPrincipal(prin) ||
|
|
|
|
prin->GetAppStatus() == nsIPrincipal::APP_STATUS_CERTIFIED;
|
|
|
|
}
|
|
|
|
|
2013-09-26 22:09:16 +04:00
|
|
|
void
|
|
|
|
Promise::MaybeResolve(JSContext* aCx,
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Handle<JS::Value> aValue)
|
2013-09-26 22:09:16 +04:00
|
|
|
{
|
|
|
|
MaybeResolveInternal(aCx, aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Promise::MaybeReject(JSContext* aCx,
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Handle<JS::Value> aValue)
|
2013-09-26 22:09:16 +04:00
|
|
|
{
|
|
|
|
MaybeRejectInternal(aCx, aValue);
|
|
|
|
}
|
|
|
|
|
2013-06-26 17:44:21 +04:00
|
|
|
static void
|
|
|
|
EnterCompartment(Maybe<JSAutoCompartment>& aAc, JSContext* aCx,
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Handle<JS::Value> aValue)
|
2013-06-26 17:44:21 +04:00
|
|
|
{
|
|
|
|
// FIXME Bug 878849
|
2013-11-19 22:39:51 +04:00
|
|
|
if (aValue.isObject()) {
|
|
|
|
JS::Rooted<JSObject*> rooted(aCx, &aValue.toObject());
|
2013-06-26 17:44:21 +04:00
|
|
|
aAc.construct(aCx, rooted);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-11 20:03:04 +04:00
|
|
|
enum {
|
|
|
|
SLOT_PROMISE = 0,
|
|
|
|
SLOT_TASK
|
|
|
|
};
|
|
|
|
|
|
|
|
/* static */ bool
|
|
|
|
Promise::JSCallback(JSContext *aCx, unsigned aArgc, JS::Value *aVp)
|
|
|
|
{
|
|
|
|
JS::CallArgs args = CallArgsFromVp(aArgc, aVp);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> v(aCx,
|
|
|
|
js::GetFunctionNativeReserved(&args.callee(),
|
|
|
|
SLOT_PROMISE));
|
|
|
|
MOZ_ASSERT(v.isObject());
|
|
|
|
|
|
|
|
Promise* promise;
|
2013-11-21 16:51:16 +04:00
|
|
|
if (NS_FAILED(UNWRAP_OBJECT(Promise, &v.toObject(), promise))) {
|
2013-09-11 20:03:04 +04:00
|
|
|
return Throw(aCx, NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
v = js::GetFunctionNativeReserved(&args.callee(), SLOT_TASK);
|
|
|
|
PromiseCallback::Task task = static_cast<PromiseCallback::Task>(v.toInt32());
|
|
|
|
|
|
|
|
if (task == PromiseCallback::Resolve) {
|
2013-11-19 22:39:51 +04:00
|
|
|
promise->MaybeResolveInternal(aCx, args.get(0));
|
2013-09-11 20:03:04 +04:00
|
|
|
} else {
|
2013-11-19 22:39:51 +04:00
|
|
|
promise->MaybeRejectInternal(aCx, args.get(0));
|
2013-09-11 20:03:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ JSObject*
|
|
|
|
Promise::CreateFunction(JSContext* aCx, JSObject* aParent, Promise* aPromise,
|
|
|
|
int32_t aTask)
|
|
|
|
{
|
|
|
|
JSFunction* func = js::NewFunctionWithReserved(aCx, JSCallback,
|
|
|
|
1 /* nargs */, 0 /* flags */,
|
|
|
|
aParent, nullptr);
|
|
|
|
if (!func) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, JS_GetFunctionObject(func));
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> promiseObj(aCx);
|
|
|
|
if (!dom::WrapNewBindingObject(aCx, obj, aPromise, &promiseObj)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
js::SetFunctionNativeReserved(obj, SLOT_PROMISE, promiseObj);
|
|
|
|
js::SetFunctionNativeReserved(obj, SLOT_TASK, JS::Int32Value(aTask));
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
/* static */ already_AddRefed<Promise>
|
2013-08-23 09:17:08 +04:00
|
|
|
Promise::Constructor(const GlobalObject& aGlobal,
|
2013-07-12 00:40:36 +04:00
|
|
|
PromiseInit& aInit, ErrorResult& aRv)
|
2013-06-12 05:41:21 +04:00
|
|
|
{
|
2013-08-23 09:17:08 +04:00
|
|
|
JSContext* cx = aGlobal.GetContext();
|
2013-11-24 23:26:07 +04:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window;
|
|
|
|
|
|
|
|
// On workers, let the window be null.
|
|
|
|
if (MOZ_LIKELY(NS_IsMainThread())) {
|
|
|
|
window = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!window) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-06-12 05:41:21 +04:00
|
|
|
}
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
nsRefPtr<Promise> promise = new Promise(window);
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2013-09-11 20:03:04 +04:00
|
|
|
JS::Rooted<JSObject*> resolveFunc(cx,
|
|
|
|
CreateFunction(cx, aGlobal.Get(), promise,
|
|
|
|
PromiseCallback::Resolve));
|
|
|
|
if (!resolveFunc) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> rejectFunc(cx,
|
|
|
|
CreateFunction(cx, aGlobal.Get(), promise,
|
|
|
|
PromiseCallback::Reject));
|
|
|
|
if (!rejectFunc) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
aInit.Call(promise, resolveFunc, rejectFunc, aRv,
|
2013-06-12 05:41:21 +04:00
|
|
|
CallbackObject::eRethrowExceptions);
|
|
|
|
aRv.WouldReportJSException();
|
|
|
|
|
|
|
|
if (aRv.IsJSException()) {
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Rooted<JS::Value> value(cx);
|
|
|
|
aRv.StealJSException(cx, &value);
|
2013-06-26 17:44:21 +04:00
|
|
|
|
|
|
|
Maybe<JSAutoCompartment> ac;
|
2013-08-23 09:17:08 +04:00
|
|
|
EnterCompartment(ac, cx, value);
|
2013-09-26 22:09:16 +04:00
|
|
|
promise->MaybeRejectInternal(cx, value);
|
2013-06-12 05:41:21 +04:00
|
|
|
}
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
return promise.forget();
|
2013-06-12 05:41:21 +04:00
|
|
|
}
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
/* static */ already_AddRefed<Promise>
|
|
|
|
Promise::Resolve(const GlobalObject& aGlobal, JSContext* aCx,
|
2013-11-20 01:29:47 +04:00
|
|
|
const Optional<JS::Handle<JS::Value>>& aValue, ErrorResult& aRv)
|
2013-06-12 05:41:22 +04:00
|
|
|
{
|
2013-11-24 23:26:07 +04:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window;
|
|
|
|
if (MOZ_LIKELY(NS_IsMainThread())) {
|
|
|
|
window = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!window) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-06-12 05:41:22 +04:00
|
|
|
}
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
nsRefPtr<Promise> promise = new Promise(window);
|
2013-06-12 05:41:22 +04:00
|
|
|
|
2013-11-20 01:29:47 +04:00
|
|
|
promise->MaybeResolveInternal(aCx,
|
|
|
|
aValue.WasPassed() ? aValue.Value() : JS::UndefinedHandleValue);
|
2013-07-12 00:40:36 +04:00
|
|
|
return promise.forget();
|
2013-06-12 05:41:22 +04:00
|
|
|
}
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
/* static */ already_AddRefed<Promise>
|
|
|
|
Promise::Reject(const GlobalObject& aGlobal, JSContext* aCx,
|
2013-11-20 01:29:47 +04:00
|
|
|
const Optional<JS::Handle<JS::Value>>& aValue, ErrorResult& aRv)
|
2013-06-12 05:41:22 +04:00
|
|
|
{
|
2013-11-24 23:26:07 +04:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window;
|
|
|
|
if (MOZ_LIKELY(NS_IsMainThread())) {
|
|
|
|
window = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!window) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-06-12 05:41:22 +04:00
|
|
|
}
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
nsRefPtr<Promise> promise = new Promise(window);
|
2013-06-12 05:41:22 +04:00
|
|
|
|
2013-11-20 01:29:47 +04:00
|
|
|
promise->MaybeRejectInternal(aCx,
|
|
|
|
aValue.WasPassed() ? aValue.Value() : JS::UndefinedHandleValue);
|
2013-07-12 00:40:36 +04:00
|
|
|
return promise.forget();
|
2013-06-12 05:41:22 +04:00
|
|
|
}
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
already_AddRefed<Promise>
|
2013-11-20 01:29:47 +04:00
|
|
|
Promise::Then(const Optional<nsRefPtr<AnyCallback>>& aResolveCallback,
|
|
|
|
const Optional<nsRefPtr<AnyCallback>>& aRejectCallback)
|
2013-06-12 05:41:22 +04:00
|
|
|
{
|
2013-07-12 00:40:36 +04:00
|
|
|
nsRefPtr<Promise> promise = new Promise(GetParentObject());
|
2013-06-12 05:41:22 +04:00
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
nsRefPtr<PromiseCallback> resolveCb =
|
2013-09-11 20:03:04 +04:00
|
|
|
PromiseCallback::Factory(promise,
|
2013-07-16 17:56:24 +04:00
|
|
|
aResolveCallback.WasPassed()
|
2013-11-20 01:29:47 +04:00
|
|
|
? aResolveCallback.Value()
|
2013-07-16 17:56:24 +04:00
|
|
|
: nullptr,
|
2013-07-12 00:40:36 +04:00
|
|
|
PromiseCallback::Resolve);
|
2013-06-12 05:41:22 +04:00
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
nsRefPtr<PromiseCallback> rejectCb =
|
2013-09-11 20:03:04 +04:00
|
|
|
PromiseCallback::Factory(promise,
|
2013-07-16 17:56:24 +04:00
|
|
|
aRejectCallback.WasPassed()
|
2013-11-20 01:29:47 +04:00
|
|
|
? aRejectCallback.Value()
|
2013-07-16 17:56:24 +04:00
|
|
|
: nullptr,
|
2013-07-12 00:40:36 +04:00
|
|
|
PromiseCallback::Reject);
|
2013-06-12 05:41:22 +04:00
|
|
|
|
|
|
|
AppendCallbacks(resolveCb, rejectCb);
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
return promise.forget();
|
2013-06-12 05:41:22 +04:00
|
|
|
}
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
already_AddRefed<Promise>
|
2013-11-20 01:29:47 +04:00
|
|
|
Promise::Catch(const Optional<nsRefPtr<AnyCallback>>& aRejectCallback)
|
2013-06-12 05:41:22 +04:00
|
|
|
{
|
2013-11-20 01:29:47 +04:00
|
|
|
Optional<nsRefPtr<AnyCallback>> resolveCb;
|
2013-07-16 17:56:24 +04:00
|
|
|
return Then(resolveCb, aRejectCallback);
|
2013-06-12 05:41:21 +04:00
|
|
|
}
|
|
|
|
|
2013-11-19 22:43:51 +04:00
|
|
|
void
|
|
|
|
Promise::AppendNativeHandler(PromiseNativeHandler* aRunnable)
|
|
|
|
{
|
|
|
|
nsRefPtr<PromiseCallback> resolveCb =
|
|
|
|
new NativePromiseCallback(aRunnable, Resolved);
|
|
|
|
|
|
|
|
nsRefPtr<PromiseCallback> rejectCb =
|
|
|
|
new NativePromiseCallback(aRunnable, Rejected);
|
|
|
|
|
|
|
|
AppendCallbacks(resolveCb, rejectCb);
|
|
|
|
}
|
|
|
|
|
2013-06-12 05:41:21 +04:00
|
|
|
void
|
2013-07-12 00:40:36 +04:00
|
|
|
Promise::AppendCallbacks(PromiseCallback* aResolveCallback,
|
|
|
|
PromiseCallback* aRejectCallback)
|
2013-06-12 05:41:21 +04:00
|
|
|
{
|
2013-06-12 05:41:22 +04:00
|
|
|
if (aResolveCallback) {
|
|
|
|
mResolveCallbacks.AppendElement(aResolveCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aRejectCallback) {
|
2013-08-29 08:30:06 +04:00
|
|
|
mHadRejectCallback = true;
|
2013-06-12 05:41:22 +04:00
|
|
|
mRejectCallbacks.AppendElement(aRejectCallback);
|
|
|
|
}
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2013-09-11 20:03:04 +04:00
|
|
|
// If promise's state is resolved, queue a task to process our resolve
|
|
|
|
// callbacks with promise's result. If promise's state is rejected, queue a
|
|
|
|
// task to process our reject callbacks with promise's result.
|
2013-06-12 05:41:21 +04:00
|
|
|
if (mState != Pending && !mTaskPending) {
|
2013-11-24 23:26:07 +04:00
|
|
|
if (MOZ_LIKELY(NS_IsMainThread())) {
|
|
|
|
nsRefPtr<PromiseTask> task = new PromiseTask(this);
|
|
|
|
NS_DispatchToCurrentThread(task);
|
|
|
|
} else {
|
|
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
nsRefPtr<WorkerPromiseTask> task = new WorkerPromiseTask(worker, this);
|
|
|
|
worker->Dispatch(task);
|
|
|
|
}
|
2013-06-12 05:41:21 +04:00
|
|
|
mTaskPending = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-07-12 00:40:36 +04:00
|
|
|
Promise::RunTask()
|
2013-06-12 05:41:21 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mState != Pending);
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
nsTArray<nsRefPtr<PromiseCallback> > callbacks;
|
2013-06-12 05:41:21 +04:00
|
|
|
callbacks.SwapElements(mState == Resolved ? mResolveCallbacks
|
|
|
|
: mRejectCallbacks);
|
|
|
|
mResolveCallbacks.Clear();
|
|
|
|
mRejectCallbacks.Clear();
|
|
|
|
|
2013-11-24 23:26:07 +04:00
|
|
|
JSContext* cx = nsContentUtils::GetDefaultJSContextForThread();
|
2013-11-19 22:39:51 +04:00
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
JS::Rooted<JS::Value> value(cx, mResult);
|
2013-06-12 05:41:21 +04:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < callbacks.Length(); ++i) {
|
2013-06-12 05:41:22 +04:00
|
|
|
callbacks[i]->Call(value);
|
2013-06-12 05:41:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-29 08:30:06 +04:00
|
|
|
void
|
|
|
|
Promise::MaybeReportRejected()
|
|
|
|
{
|
|
|
|
if (mState != Rejected || mHadRejectCallback || mResult.isUndefined()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSErrorReport* report = js::ErrorFromException(mResult);
|
|
|
|
if (!report) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mResult.isObject(), "How did we get a JSErrorReport?");
|
|
|
|
|
2013-11-24 23:26:07 +04:00
|
|
|
// Remains null in case of worker.
|
|
|
|
nsCOMPtr<nsPIDOMWindow> win;
|
|
|
|
bool isChromeError = false;
|
|
|
|
|
|
|
|
if (MOZ_LIKELY(NS_IsMainThread())) {
|
|
|
|
win =
|
|
|
|
do_QueryInterface(nsJSUtils::GetStaticScriptGlobal(&mResult.toObject()));
|
|
|
|
nsIPrincipal* principal = nsContentUtils::GetObjectPrincipal(&mResult.toObject());
|
|
|
|
isChromeError = nsContentUtils::IsSystemPrincipal(principal);
|
|
|
|
} else {
|
|
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
isChromeError = worker->IsChromeWorker();
|
|
|
|
}
|
2013-08-29 08:30:06 +04:00
|
|
|
|
|
|
|
// Now post an event to do the real reporting async
|
2014-01-11 02:07:46 +04:00
|
|
|
// Since Promises preserve their wrapper, it is essential to nsRefPtr<> the
|
|
|
|
// AsyncErrorReporter, otherwise if the call to DispatchToMainThread fails, it
|
|
|
|
// will leak. See Bug 958684.
|
|
|
|
nsRefPtr<AsyncErrorReporter> r =
|
2013-08-29 08:30:06 +04:00
|
|
|
new AsyncErrorReporter(JS_GetObjectRuntime(&mResult.toObject()),
|
|
|
|
report,
|
|
|
|
nullptr,
|
2013-11-24 23:26:07 +04:00
|
|
|
isChromeError,
|
2014-01-11 02:07:46 +04:00
|
|
|
win);
|
|
|
|
NS_DispatchToMainThread(r);
|
2013-08-29 08:30:06 +04:00
|
|
|
}
|
|
|
|
|
2013-09-11 20:03:04 +04:00
|
|
|
void
|
2013-09-26 22:09:16 +04:00
|
|
|
Promise::MaybeResolveInternal(JSContext* aCx,
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Handle<JS::Value> aValue,
|
2013-09-26 22:09:16 +04:00
|
|
|
PromiseTaskSync aAsynchronous)
|
2013-09-11 20:03:04 +04:00
|
|
|
{
|
|
|
|
if (mResolvePending) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResolveInternal(aCx, aValue, aAsynchronous);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-09-26 22:09:16 +04:00
|
|
|
Promise::MaybeRejectInternal(JSContext* aCx,
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Handle<JS::Value> aValue,
|
2013-09-26 22:09:16 +04:00
|
|
|
PromiseTaskSync aAsynchronous)
|
2013-09-11 20:03:04 +04:00
|
|
|
{
|
|
|
|
if (mResolvePending) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RejectInternal(aCx, aValue, aAsynchronous);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Promise::ResolveInternal(JSContext* aCx,
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Handle<JS::Value> aValue,
|
2013-09-11 20:03:04 +04:00
|
|
|
PromiseTaskSync aAsynchronous)
|
|
|
|
{
|
|
|
|
mResolvePending = true;
|
|
|
|
|
|
|
|
// TODO: Bug 879245 - Then-able objects
|
2013-11-19 22:39:51 +04:00
|
|
|
if (aValue.isObject()) {
|
|
|
|
JS::Rooted<JSObject*> valueObj(aCx, &aValue.toObject());
|
2013-09-11 20:03:04 +04:00
|
|
|
Promise* nextPromise;
|
2013-11-21 16:51:16 +04:00
|
|
|
nsresult rv = UNWRAP_OBJECT(Promise, valueObj, nextPromise);
|
2013-09-11 20:03:04 +04:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsRefPtr<PromiseCallback> resolveCb = new ResolvePromiseCallback(this);
|
|
|
|
nsRefPtr<PromiseCallback> rejectCb = new RejectPromiseCallback(this);
|
|
|
|
nextPromise->AppendCallbacks(resolveCb, rejectCb);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the synchronous flag is set, process our resolve callbacks with
|
|
|
|
// value. Otherwise, the synchronous flag is unset, queue a task to process
|
|
|
|
// own resolve callbacks with value. Otherwise, the synchronous flag is
|
|
|
|
// unset, queue a task to process our resolve callbacks with value.
|
2013-11-19 22:39:51 +04:00
|
|
|
RunResolveTask(aValue, Resolved, aAsynchronous);
|
2013-09-11 20:03:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Promise::RejectInternal(JSContext* aCx,
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Handle<JS::Value> aValue,
|
2013-09-11 20:03:04 +04:00
|
|
|
PromiseTaskSync aAsynchronous)
|
|
|
|
{
|
|
|
|
mResolvePending = true;
|
|
|
|
|
|
|
|
// If the synchronous flag is set, process our reject callbacks with
|
|
|
|
// value. Otherwise, the synchronous flag is unset, queue a task to process
|
|
|
|
// promise's reject callbacks with value.
|
2013-11-19 22:39:51 +04:00
|
|
|
RunResolveTask(aValue, Rejected, aAsynchronous);
|
2013-09-11 20:03:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Promise::RunResolveTask(JS::Handle<JS::Value> aValue,
|
|
|
|
PromiseState aState,
|
|
|
|
PromiseTaskSync aAsynchronous)
|
|
|
|
{
|
|
|
|
// If the synchronous flag is unset, queue a task to process our
|
|
|
|
// accept callbacks with value.
|
|
|
|
if (aAsynchronous == AsyncTask) {
|
2013-11-24 23:26:07 +04:00
|
|
|
if (MOZ_LIKELY(NS_IsMainThread())) {
|
|
|
|
nsRefPtr<PromiseResolverTask> task =
|
|
|
|
new PromiseResolverTask(this, aValue, aState);
|
|
|
|
NS_DispatchToCurrentThread(task);
|
|
|
|
} else {
|
|
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
nsRefPtr<WorkerPromiseResolverTask> task =
|
|
|
|
new WorkerPromiseResolverTask(worker, this, aValue, aState);
|
|
|
|
worker->Dispatch(task);
|
|
|
|
}
|
2013-09-11 20:03:04 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetResult(aValue);
|
|
|
|
SetState(aState);
|
|
|
|
RunTask();
|
|
|
|
}
|
|
|
|
|
2013-06-12 05:41:21 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|