2013-06-11 18:41:21 -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/. */
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
#include "mozilla/dom/PromiseBinding.h"
|
|
|
|
#include "mozilla/dom/PromiseResolver.h"
|
2013-06-12 06:44:56 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-07-11 13:40:36 -07:00
|
|
|
#include "PromiseCallback.h"
|
2013-06-11 18:41:21 -07:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2013-08-07 14:40:20 -07:00
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
#include "nsJSPrincipals.h"
|
2013-06-11 18:41:21 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
// PromiseTask
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
// This class processes the promise's callbacks with promise's result.
|
|
|
|
class PromiseTask MOZ_FINAL : public nsRunnable
|
2013-06-11 18:41:21 -07:00
|
|
|
{
|
|
|
|
public:
|
2013-07-11 13:40:36 -07:00
|
|
|
PromiseTask(Promise* aPromise)
|
|
|
|
: mPromise(aPromise)
|
2013-06-11 18:41:21 -07:00
|
|
|
{
|
2013-07-11 13:40:36 -07:00
|
|
|
MOZ_ASSERT(aPromise);
|
|
|
|
MOZ_COUNT_CTOR(PromiseTask);
|
2013-06-11 18:41:21 -07:00
|
|
|
}
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
~PromiseTask()
|
2013-06-11 18:41:21 -07:00
|
|
|
{
|
2013-07-11 13:40:36 -07:00
|
|
|
MOZ_COUNT_DTOR(PromiseTask);
|
2013-06-11 18:41:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2013-07-11 13:40:36 -07:00
|
|
|
mPromise->mTaskPending = false;
|
|
|
|
mPromise->RunTask();
|
2013-06-11 18:41:21 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-07-11 13:40:36 -07:00
|
|
|
nsRefPtr<Promise> mPromise;
|
2013-06-11 18:41:21 -07:00
|
|
|
};
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
// Promise
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(Promise)
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Promise)
|
2013-06-11 18:41:21 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mResolver)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mResolveCallbacks);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mRejectCallbacks);
|
|
|
|
tmp->mResult = JSVAL_VOID;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Promise)
|
2013-06-11 18:41:21 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mResolver)
|
|
|
|
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-11 13:40:36 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(Promise)
|
2013-06-11 18:41:21 -07: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-11 13:40:36 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Promise)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Promise)
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Promise)
|
2013-06-11 18:41:21 -07:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
Promise::Promise(nsPIDOMWindow* aWindow)
|
2013-06-11 18:41:21 -07:00
|
|
|
: mWindow(aWindow)
|
|
|
|
, mResult(JS::UndefinedValue())
|
|
|
|
, mState(Pending)
|
|
|
|
, mTaskPending(false)
|
|
|
|
{
|
2013-07-11 13:40:36 -07:00
|
|
|
MOZ_COUNT_CTOR(Promise);
|
|
|
|
NS_HOLD_JS_OBJECTS(this, Promise);
|
2013-06-11 18:41:21 -07:00
|
|
|
SetIsDOMBinding();
|
2013-06-11 18:41:22 -07:00
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
mResolver = new PromiseResolver(this);
|
2013-06-11 18:41:21 -07:00
|
|
|
}
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
Promise::~Promise()
|
2013-06-11 18:41:21 -07:00
|
|
|
{
|
|
|
|
mResult = JSVAL_VOID;
|
2013-07-11 13:40:36 -07:00
|
|
|
NS_DROP_JS_OBJECTS(this, Promise);
|
|
|
|
MOZ_COUNT_DTOR(Promise);
|
2013-06-11 18:41:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2013-07-11 13:40:36 -07:00
|
|
|
Promise::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
2013-06-11 18:41:21 -07:00
|
|
|
{
|
2013-07-11 13:40:36 -07:00
|
|
|
return PromiseBinding::Wrap(aCx, aScope, this);
|
2013-06-11 18:41:21 -07:00
|
|
|
}
|
|
|
|
|
2013-06-12 06:44:56 -07:00
|
|
|
/* static */ bool
|
2013-07-11 13:40:36 -07:00
|
|
|
Promise::PrefEnabled()
|
2013-06-12 06:44:56 -07:00
|
|
|
{
|
2013-07-11 13:40:36 -07:00
|
|
|
return Preferences::GetBool("dom.promise.enabled", false);
|
2013-06-12 06:44:56 -07:00
|
|
|
}
|
|
|
|
|
2013-08-07 14:40:20 -07:00
|
|
|
/* static */ bool
|
|
|
|
Promise::EnabledForScope(JSContext* aCx, JSObject* /* unused */)
|
|
|
|
{
|
|
|
|
// Enable if the pref is enabled or if we're chrome or if we're a
|
|
|
|
// certified app.
|
|
|
|
if (PrefEnabled()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that we have no concept of a certified app in workers.
|
|
|
|
// XXXbz well, why not?
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
return workers::GetWorkerPrivateFromContext(aCx)->IsChromeWorker();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIPrincipal* prin = nsContentUtils::GetSubjectPrincipal();
|
|
|
|
return nsContentUtils::IsSystemPrincipal(prin) ||
|
|
|
|
prin->GetAppStatus() == nsIPrincipal::APP_STATUS_CERTIFIED;
|
|
|
|
}
|
|
|
|
|
2013-06-26 06:44:21 -07:00
|
|
|
static void
|
|
|
|
EnterCompartment(Maybe<JSAutoCompartment>& aAc, JSContext* aCx,
|
|
|
|
const Optional<JS::Handle<JS::Value> >& aValue)
|
|
|
|
{
|
|
|
|
// FIXME Bug 878849
|
|
|
|
if (aValue.WasPassed() && aValue.Value().isObject()) {
|
|
|
|
JS::Rooted<JSObject*> rooted(aCx, &aValue.Value().toObject());
|
|
|
|
aAc.construct(aCx, rooted);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
/* static */ already_AddRefed<Promise>
|
2013-08-22 22:17:08 -07:00
|
|
|
Promise::Constructor(const GlobalObject& aGlobal,
|
2013-07-11 13:40:36 -07:00
|
|
|
PromiseInit& aInit, ErrorResult& aRv)
|
2013-06-11 18:41:21 -07:00
|
|
|
{
|
2013-08-22 22:17:08 -07:00
|
|
|
JSContext* cx = aGlobal.GetContext();
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
|
2013-06-11 18:41:21 -07:00
|
|
|
if (!window) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
nsRefPtr<Promise> promise = new Promise(window);
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
aInit.Call(promise, *promise->mResolver, aRv,
|
2013-06-11 18:41:21 -07:00
|
|
|
CallbackObject::eRethrowExceptions);
|
|
|
|
aRv.WouldReportJSException();
|
|
|
|
|
|
|
|
if (aRv.IsJSException()) {
|
2013-08-22 22:17:08 -07:00
|
|
|
Optional<JS::Handle<JS::Value> > value(cx);
|
|
|
|
aRv.StealJSException(cx, &value.Value());
|
2013-06-26 06:44:21 -07:00
|
|
|
|
|
|
|
Maybe<JSAutoCompartment> ac;
|
2013-08-22 22:17:08 -07:00
|
|
|
EnterCompartment(ac, cx, value);
|
|
|
|
promise->mResolver->Reject(cx, value);
|
2013-06-11 18:41:21 -07:00
|
|
|
}
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
return promise.forget();
|
2013-06-11 18:41:21 -07:00
|
|
|
}
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
/* static */ already_AddRefed<Promise>
|
|
|
|
Promise::Resolve(const GlobalObject& aGlobal, JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue, ErrorResult& aRv)
|
2013-06-11 18:41:22 -07:00
|
|
|
{
|
2013-08-22 22:17:08 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
|
2013-06-11 18:41:22 -07:00
|
|
|
if (!window) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
nsRefPtr<Promise> promise = new Promise(window);
|
2013-06-11 18:41:22 -07:00
|
|
|
|
|
|
|
Optional<JS::Handle<JS::Value> > value(aCx, aValue);
|
2013-07-11 13:40:36 -07:00
|
|
|
promise->mResolver->Resolve(aCx, value);
|
|
|
|
return promise.forget();
|
2013-06-11 18:41:22 -07:00
|
|
|
}
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
/* static */ already_AddRefed<Promise>
|
|
|
|
Promise::Reject(const GlobalObject& aGlobal, JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue, ErrorResult& aRv)
|
2013-06-11 18:41:22 -07:00
|
|
|
{
|
2013-08-22 22:17:08 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
|
2013-06-11 18:41:22 -07:00
|
|
|
if (!window) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
nsRefPtr<Promise> promise = new Promise(window);
|
2013-06-11 18:41:22 -07:00
|
|
|
|
|
|
|
Optional<JS::Handle<JS::Value> > value(aCx, aValue);
|
2013-07-11 13:40:36 -07:00
|
|
|
promise->mResolver->Reject(aCx, value);
|
|
|
|
return promise.forget();
|
2013-06-11 18:41:22 -07:00
|
|
|
}
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
already_AddRefed<Promise>
|
2013-07-16 06:56:24 -07:00
|
|
|
Promise::Then(const Optional<OwningNonNull<AnyCallback> >& aResolveCallback,
|
|
|
|
const Optional<OwningNonNull<AnyCallback> >& aRejectCallback)
|
2013-06-11 18:41:22 -07:00
|
|
|
{
|
2013-07-11 13:40:36 -07:00
|
|
|
nsRefPtr<Promise> promise = new Promise(GetParentObject());
|
2013-06-11 18:41:22 -07:00
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
nsRefPtr<PromiseCallback> resolveCb =
|
|
|
|
PromiseCallback::Factory(promise->mResolver,
|
2013-07-16 06:56:24 -07:00
|
|
|
aResolveCallback.WasPassed()
|
|
|
|
? &aResolveCallback.Value()
|
|
|
|
: nullptr,
|
2013-07-11 13:40:36 -07:00
|
|
|
PromiseCallback::Resolve);
|
2013-06-11 18:41:22 -07:00
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
nsRefPtr<PromiseCallback> rejectCb =
|
|
|
|
PromiseCallback::Factory(promise->mResolver,
|
2013-07-16 06:56:24 -07:00
|
|
|
aRejectCallback.WasPassed()
|
|
|
|
? &aRejectCallback.Value()
|
|
|
|
: nullptr,
|
2013-07-11 13:40:36 -07:00
|
|
|
PromiseCallback::Reject);
|
2013-06-11 18:41:22 -07:00
|
|
|
|
|
|
|
AppendCallbacks(resolveCb, rejectCb);
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
return promise.forget();
|
2013-06-11 18:41:22 -07:00
|
|
|
}
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
already_AddRefed<Promise>
|
2013-07-16 06:56:24 -07:00
|
|
|
Promise::Catch(const Optional<OwningNonNull<AnyCallback> >& aRejectCallback)
|
2013-06-11 18:41:22 -07:00
|
|
|
{
|
2013-07-16 06:56:24 -07:00
|
|
|
Optional<OwningNonNull<AnyCallback> > resolveCb;
|
|
|
|
return Then(resolveCb, aRejectCallback);
|
2013-06-11 18:41:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-07-11 13:40:36 -07:00
|
|
|
Promise::AppendCallbacks(PromiseCallback* aResolveCallback,
|
|
|
|
PromiseCallback* aRejectCallback)
|
2013-06-11 18:41:21 -07:00
|
|
|
{
|
2013-06-11 18:41:22 -07:00
|
|
|
if (aResolveCallback) {
|
|
|
|
mResolveCallbacks.AppendElement(aResolveCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aRejectCallback) {
|
|
|
|
mRejectCallbacks.AppendElement(aRejectCallback);
|
|
|
|
}
|
2013-06-11 18:41:21 -07:00
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
// If promise's state is resolved, queue a task to process promise's resolve
|
|
|
|
// callbacks with promise's result. If promise's state is rejected, queue a task
|
|
|
|
// to process promise's reject callbacks with promise's result.
|
2013-06-11 18:41:21 -07:00
|
|
|
if (mState != Pending && !mTaskPending) {
|
2013-07-11 13:40:36 -07:00
|
|
|
nsRefPtr<PromiseTask> task = new PromiseTask(this);
|
2013-06-11 18:41:21 -07:00
|
|
|
NS_DispatchToCurrentThread(task);
|
|
|
|
mTaskPending = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-07-11 13:40:36 -07:00
|
|
|
Promise::RunTask()
|
2013-06-11 18:41:21 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mState != Pending);
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
nsTArray<nsRefPtr<PromiseCallback> > callbacks;
|
2013-06-11 18:41:21 -07:00
|
|
|
callbacks.SwapElements(mState == Resolved ? mResolveCallbacks
|
|
|
|
: mRejectCallbacks);
|
|
|
|
mResolveCallbacks.Clear();
|
|
|
|
mRejectCallbacks.Clear();
|
|
|
|
|
2013-06-20 11:05:34 -07:00
|
|
|
JSAutoRequest ar(nsContentUtils::GetSafeJSContext());
|
|
|
|
Optional<JS::Handle<JS::Value> > value(nsContentUtils::GetSafeJSContext(), mResult);
|
2013-06-11 18:41:21 -07:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < callbacks.Length(); ++i) {
|
2013-06-11 18:41:22 -07:00
|
|
|
callbacks[i]->Call(value);
|
2013-06-11 18:41:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|