2015-05-03 22:32:37 +03: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-06-12 05:41:21 +04: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/. */
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2018-08-18 19:43:22 +03:00
|
|
|
#include "mozilla/dom/Promise-inl.h"
|
2013-08-29 08:30:06 +04:00
|
|
|
|
2014-11-17 12:42:00 +03:00
|
|
|
#include "js/Debug.h"
|
2015-08-06 15:45:21 +03:00
|
|
|
|
|
|
|
#include "mozilla/Atomics.h"
|
2016-09-14 16:47:32 +03:00
|
|
|
#include "mozilla/CycleCollectedJSContext.h"
|
2018-10-10 00:42:22 +03:00
|
|
|
#include "mozilla/EventStateManager.h"
|
2015-08-06 15:45:21 +03:00
|
|
|
#include "mozilla/OwningNonNull.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
2018-08-18 19:43:22 +03:00
|
|
|
#include "mozilla/ResultExtensions.h"
|
2018-10-10 00:42:22 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-08-06 15:45:21 +03:00
|
|
|
|
2014-07-19 05:31:11 +04:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2016-04-20 02:29:21 +03:00
|
|
|
#include "mozilla/dom/DOMException.h"
|
|
|
|
#include "mozilla/dom/DOMExceptionBinding.h"
|
2015-08-06 15:45:21 +03:00
|
|
|
#include "mozilla/dom/MediaStreamError.h"
|
2013-07-12 00:40:36 +04:00
|
|
|
#include "mozilla/dom/PromiseBinding.h"
|
2014-07-19 05:31:11 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2018-01-31 10:25:30 +03:00
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
|
|
|
#include "mozilla/dom/WorkerRunnable.h"
|
2018-03-14 17:26:33 +03:00
|
|
|
#include "mozilla/dom/WorkerRef.h"
|
2015-08-06 15:45:21 +03:00
|
|
|
|
|
|
|
#include "jsfriendapi.h"
|
2015-09-02 19:20:30 +03:00
|
|
|
#include "js/StructuredClone.h"
|
2015-08-06 15:45:21 +03:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsGlobalWindow.h"
|
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
|
|
|
#include "nsJSEnvironment.h"
|
|
|
|
#include "nsJSPrincipals.h"
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2015-04-10 18:27:57 +03:00
|
|
|
#include "PromiseDebugging.h"
|
2013-11-19 22:43:51 +04:00
|
|
|
#include "PromiseNativeHandler.h"
|
2014-02-24 17:56:54 +04:00
|
|
|
#include "PromiseWorkerProxy.h"
|
2015-11-25 23:48:09 +03:00
|
|
|
#include "WrapperFactory.h"
|
2014-07-04 09:24:59 +04:00
|
|
|
#include "xpcpublic.h"
|
2019-03-26 22:34:29 +03:00
|
|
|
#include "xpcprivate.h"
|
2013-06-12 05:41:21 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2015-04-10 18:27:57 +03:00
|
|
|
namespace {
|
|
|
|
// Generator used by Promise::GetID.
|
|
|
|
Atomic<uintptr_t> gIDGenerator(0);
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-04-10 18:27:57 +03: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)
|
2014-02-26 01:34:55 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mGlobal)
|
2016-02-10 01:40:31 +03:00
|
|
|
tmp->mPromiseObj = nullptr;
|
2013-06-12 05:41:21 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Promise)
|
2014-02-26 01:34:55 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGlobal)
|
2013-06-12 05:41:21 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(Promise)
|
2016-02-10 01:40:31 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mPromiseObj);
|
2013-06-12 05:41:21 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2018-05-17 00:54:04 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Promise)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Promise)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Promise)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(Promise)
|
|
|
|
NS_INTERFACE_MAP_END
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2014-02-26 01:34:55 +04:00
|
|
|
Promise::Promise(nsIGlobalObject* aGlobal)
|
2016-02-10 01:40:31 +03:00
|
|
|
: mGlobal(aGlobal), mPromiseObj(nullptr) {
|
2014-02-26 01:34:55 +04:00
|
|
|
MOZ_ASSERT(mGlobal);
|
|
|
|
|
2013-08-17 00:10:17 +04:00
|
|
|
mozilla::HoldJSObjects(this);
|
2013-06-12 05:41:21 +04:00
|
|
|
}
|
|
|
|
|
2013-08-17 00:10:17 +04:00
|
|
|
Promise::~Promise() { mozilla::DropJSObjects(this); }
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
// static
|
|
|
|
already_AddRefed<Promise> Promise::Create(
|
2018-10-10 00:42:22 +03:00
|
|
|
nsIGlobalObject* aGlobal, ErrorResult& aRv,
|
|
|
|
PropagateUserInteraction aPropagateUserInteraction) {
|
2016-04-25 12:06:30 +03:00
|
|
|
if (!aGlobal) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-02-10 01:40:31 +03:00
|
|
|
RefPtr<Promise> p = new Promise(aGlobal);
|
2018-10-10 00:42:22 +03:00
|
|
|
p->CreateWrapper(nullptr, aRv, aPropagateUserInteraction);
|
2016-02-10 01:40:31 +03:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return p.forget();
|
|
|
|
}
|
|
|
|
|
2018-10-10 00:42:22 +03:00
|
|
|
bool Promise::MaybePropagateUserInputEventHandling() {
|
|
|
|
JS::PromiseUserInputEventHandlingState state =
|
|
|
|
EventStateManager::IsHandlingUserInput()
|
|
|
|
? JS::PromiseUserInputEventHandlingState::HadUserInteractionAtCreation
|
|
|
|
: JS::PromiseUserInputEventHandlingState::
|
|
|
|
DidntHaveUserInteractionAtCreation;
|
|
|
|
JS::Rooted<JSObject*> p(RootingCx(), mPromiseObj);
|
|
|
|
return JS::SetPromiseUserInputEventHandlingState(p, state);
|
|
|
|
}
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
// static
|
|
|
|
already_AddRefed<Promise> Promise::Resolve(
|
|
|
|
nsIGlobalObject* aGlobal, JSContext* aCx, JS::Handle<JS::Value> aValue,
|
2018-10-10 00:42:22 +03:00
|
|
|
ErrorResult& aRv, PropagateUserInteraction aPropagateUserInteraction) {
|
2018-08-02 09:49:00 +03:00
|
|
|
JSAutoRealm ar(aCx, aGlobal->GetGlobalJSObject());
|
2016-02-10 01:40:31 +03:00
|
|
|
JS::Rooted<JSObject*> p(aCx, JS::CallOriginalPromiseResolve(aCx, aValue));
|
|
|
|
if (!p) {
|
2016-03-10 12:50:56 +03:00
|
|
|
aRv.NoteJSContextException(aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-10-10 00:42:22 +03:00
|
|
|
return CreateFromExisting(aGlobal, p, aPropagateUserInteraction);
|
2016-02-10 01:40:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<Promise> Promise::Reject(nsIGlobalObject* aGlobal,
|
|
|
|
JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) {
|
2018-08-02 09:49:00 +03:00
|
|
|
JSAutoRealm ar(aCx, aGlobal->GetGlobalJSObject());
|
2016-02-10 01:40:31 +03:00
|
|
|
JS::Rooted<JSObject*> p(aCx, JS::CallOriginalPromiseReject(aCx, aValue));
|
|
|
|
if (!p) {
|
2016-03-10 12:50:56 +03:00
|
|
|
aRv.NoteJSContextException(aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-10-10 00:42:22 +03:00
|
|
|
// This promise will never be resolved, so we pass
|
|
|
|
// eDontPropagateUserInteraction for aPropagateUserInteraction
|
|
|
|
// unconditionally.
|
|
|
|
return CreateFromExisting(aGlobal, p, eDontPropagateUserInteraction);
|
2016-02-10 01:40:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<Promise> Promise::All(
|
2018-10-10 00:42:22 +03:00
|
|
|
JSContext* aCx, const nsTArray<RefPtr<Promise>>& aPromiseList,
|
|
|
|
ErrorResult& aRv, PropagateUserInteraction aPropagateUserInteraction) {
|
2018-04-14 02:31:42 +03:00
|
|
|
JS::Rooted<JSObject*> globalObj(aCx, JS::CurrentGlobalOrNull(aCx));
|
|
|
|
if (!globalObj) {
|
2018-04-13 00:03:49 +03:00
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-02-10 01:40:31 +03:00
|
|
|
|
2018-04-14 02:31:42 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = xpc::NativeGlobal(globalObj);
|
|
|
|
if (!global) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-04-14 17:25:15 +03:00
|
|
|
|
2019-03-26 17:00:51 +03:00
|
|
|
JS::RootedVector<JSObject*> promises(aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
if (!promises.reserve(aPromiseList.Length())) {
|
2018-04-14 02:31:42 +03:00
|
|
|
aRv.NoteJSContextException(aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& promise : aPromiseList) {
|
2018-04-14 02:31:42 +03:00
|
|
|
JS::Rooted<JSObject*> promiseObj(aCx, promise->PromiseObj());
|
2016-02-10 01:40:31 +03:00
|
|
|
// Just in case, make sure these are all in the context compartment.
|
2018-04-14 02:31:42 +03:00
|
|
|
if (!JS_WrapObject(aCx, &promiseObj)) {
|
|
|
|
aRv.NoteJSContextException(aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
promises.infallibleAppend(promiseObj);
|
|
|
|
}
|
|
|
|
|
2018-04-14 02:31:42 +03:00
|
|
|
JS::Rooted<JSObject*> result(aCx, JS::GetWaitForAllPromise(aCx, promises));
|
2016-02-10 01:40:31 +03:00
|
|
|
if (!result) {
|
2018-04-14 02:31:42 +03:00
|
|
|
aRv.NoteJSContextException(aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-10-10 00:42:22 +03:00
|
|
|
return CreateFromExisting(global, result, aPropagateUserInteraction);
|
2016-02-10 01:40:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Promise::Then(JSContext* aCx,
|
|
|
|
// aCalleeGlobal may not be in the compartment of aCx, when
|
|
|
|
// called over Xrays.
|
|
|
|
JS::Handle<JSObject*> aCalleeGlobal,
|
|
|
|
AnyCallback* aResolveCallback, AnyCallback* aRejectCallback,
|
|
|
|
JS::MutableHandle<JS::Value> aRetval, ErrorResult& aRv) {
|
2016-04-11 18:16:26 +03:00
|
|
|
NS_ASSERT_OWNINGTHREAD(Promise);
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
// Let's hope this does the right thing with Xrays... Ensure everything is
|
|
|
|
// just in the caller compartment; that ought to do the trick. In theory we
|
|
|
|
// should consider aCalleeGlobal, but in practice our only caller is
|
|
|
|
// DOMRequest::Then, which is not working with a Promise subclass, so things
|
|
|
|
// should be OK.
|
2016-02-10 01:40:31 +03:00
|
|
|
JS::Rooted<JSObject*> promise(aCx, PromiseObj());
|
2016-02-10 01:40:31 +03:00
|
|
|
if (!JS_WrapObject(aCx, &promise)) {
|
2016-03-10 12:50:56 +03:00
|
|
|
aRv.NoteJSContextException(aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> resolveCallback(aCx);
|
|
|
|
if (aResolveCallback) {
|
2016-11-15 08:25:37 +03:00
|
|
|
resolveCallback = aResolveCallback->CallbackOrNull();
|
2016-02-10 01:40:31 +03:00
|
|
|
if (!JS_WrapObject(aCx, &resolveCallback)) {
|
2016-03-10 12:50:56 +03:00
|
|
|
aRv.NoteJSContextException(aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> rejectCallback(aCx);
|
|
|
|
if (aRejectCallback) {
|
2016-11-15 08:25:37 +03:00
|
|
|
rejectCallback = aRejectCallback->CallbackOrNull();
|
2016-02-10 01:40:31 +03:00
|
|
|
if (!JS_WrapObject(aCx, &rejectCallback)) {
|
2016-03-10 12:50:56 +03:00
|
|
|
aRv.NoteJSContextException(aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> retval(aCx);
|
|
|
|
retval = JS::CallOriginalPromiseThen(aCx, promise, resolveCallback,
|
|
|
|
rejectCallback);
|
|
|
|
if (!retval) {
|
2016-03-10 12:50:56 +03:00
|
|
|
aRv.NoteJSContextException(aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aRetval.setObject(*retval);
|
|
|
|
}
|
|
|
|
|
2018-08-18 19:43:22 +03:00
|
|
|
void PromiseNativeThenHandlerBase::ResolvedCallback(
|
|
|
|
JSContext* aCx, JS::Handle<JS::Value> aValue) {
|
|
|
|
RefPtr<Promise> promise = CallResolveCallback(aCx, aValue);
|
2019-02-27 22:54:31 +03:00
|
|
|
if (promise) {
|
|
|
|
mPromise->MaybeResolve(promise);
|
|
|
|
} else {
|
2019-07-29 17:43:54 +03:00
|
|
|
mPromise->MaybeResolveWithUndefined();
|
2019-02-27 22:54:31 +03:00
|
|
|
}
|
2018-08-18 19:43:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PromiseNativeThenHandlerBase::RejectedCallback(
|
|
|
|
JSContext* aCx, JS::Handle<JS::Value> aValue) {
|
2019-07-29 17:43:54 +03:00
|
|
|
mPromise->MaybeReject(aValue);
|
2018-08-18 19:43:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(PromiseNativeThenHandlerBase)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(PromiseNativeThenHandlerBase)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPromise)
|
|
|
|
tmp->Traverse(cb);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(PromiseNativeThenHandlerBase)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPromise)
|
|
|
|
tmp->Unlink();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PromiseNativeThenHandlerBase)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(PromiseNativeThenHandlerBase)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(PromiseNativeThenHandlerBase)
|
|
|
|
|
|
|
|
Result<RefPtr<Promise>, nsresult> Promise::ThenWithoutCycleCollection(
|
|
|
|
const std::function<already_AddRefed<Promise>(
|
|
|
|
JSContext* aCx, JS::HandleValue aValue)>& aCallback) {
|
|
|
|
return ThenWithCycleCollectedArgs(aCallback);
|
|
|
|
}
|
|
|
|
|
2018-10-10 00:42:22 +03:00
|
|
|
void Promise::CreateWrapper(
|
|
|
|
JS::Handle<JSObject*> aDesiredProto, ErrorResult& aRv,
|
|
|
|
PropagateUserInteraction aPropagateUserInteraction) {
|
2016-02-10 01:40:31 +03:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (!jsapi.Init(mGlobal)) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
JSContext* cx = jsapi.cx();
|
2017-04-23 14:48:48 +03:00
|
|
|
mPromiseObj = JS::NewPromiseObject(cx, nullptr, aDesiredProto);
|
2016-02-10 01:40:31 +03:00
|
|
|
if (!mPromiseObj) {
|
2016-02-10 01:40:31 +03:00
|
|
|
JS_ClearPendingException(cx);
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
2018-10-10 00:42:22 +03:00
|
|
|
if (aPropagateUserInteraction == ePropagateUserInteraction) {
|
|
|
|
Unused << MaybePropagateUserInputEventHandling();
|
|
|
|
}
|
2016-02-10 01:40:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Promise::MaybeResolve(JSContext* aCx, JS::Handle<JS::Value> aValue) {
|
2016-04-11 18:16:26 +03:00
|
|
|
NS_ASSERT_OWNINGTHREAD(Promise);
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
JS::Rooted<JSObject*> p(aCx, PromiseObj());
|
2016-02-10 01:40:31 +03:00
|
|
|
if (!JS::ResolvePromise(aCx, p, aValue)) {
|
|
|
|
// Now what? There's nothing sane to do here.
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Promise::MaybeReject(JSContext* aCx, JS::Handle<JS::Value> aValue) {
|
2016-04-11 18:16:26 +03:00
|
|
|
NS_ASSERT_OWNINGTHREAD(Promise);
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
JS::Rooted<JSObject*> p(aCx, PromiseObj());
|
2016-02-10 01:40:31 +03:00
|
|
|
if (!JS::RejectPromise(aCx, p, aValue)) {
|
|
|
|
// Now what? There's nothing sane to do here.
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
#define SLOT_NATIVEHANDLER 0
|
|
|
|
#define SLOT_NATIVEHANDLER_TASK 1
|
|
|
|
|
|
|
|
enum class NativeHandlerTask : int32_t { Resolve, Reject };
|
|
|
|
|
2019-03-19 18:53:43 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT
|
2016-02-10 01:40:31 +03:00
|
|
|
static bool NativeHandlerCallback(JSContext* aCx, unsigned aArgc,
|
|
|
|
JS::Value* aVp) {
|
|
|
|
JS::CallArgs args = CallArgsFromVp(aArgc, aVp);
|
|
|
|
|
2017-07-10 23:05:24 +03:00
|
|
|
JS::Value v =
|
|
|
|
js::GetFunctionNativeReserved(&args.callee(), SLOT_NATIVEHANDLER);
|
2016-02-10 01:40:31 +03:00
|
|
|
MOZ_ASSERT(v.isObject());
|
|
|
|
|
2017-07-10 23:05:24 +03:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, &v.toObject());
|
2016-09-07 00:38:13 +03:00
|
|
|
PromiseNativeHandler* handler = nullptr;
|
2017-07-10 23:05:24 +03:00
|
|
|
if (NS_FAILED(UNWRAP_OBJECT(PromiseNativeHandler, &obj, handler))) {
|
2016-02-10 01:40:31 +03:00
|
|
|
return Throw(aCx, NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
v = js::GetFunctionNativeReserved(&args.callee(), SLOT_NATIVEHANDLER_TASK);
|
|
|
|
NativeHandlerTask task = static_cast<NativeHandlerTask>(v.toInt32());
|
|
|
|
|
|
|
|
if (task == NativeHandlerTask::Resolve) {
|
2019-03-19 18:53:43 +03:00
|
|
|
// handler is kept alive by "obj" on the stack.
|
|
|
|
MOZ_KnownLive(handler)->ResolvedCallback(aCx, args.get(0));
|
2016-02-10 01:40:31 +03:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(task == NativeHandlerTask::Reject);
|
2019-03-19 18:53:43 +03:00
|
|
|
// handler is kept alive by "obj" on the stack.
|
|
|
|
MOZ_KnownLive(handler)->RejectedCallback(aCx, args.get(0));
|
2016-02-10 01:40:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject* CreateNativeHandlerFunction(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aHolder,
|
|
|
|
NativeHandlerTask aTask) {
|
|
|
|
JSFunction* func = js::NewFunctionWithReserved(aCx, NativeHandlerCallback,
|
|
|
|
/* nargs = */ 1,
|
|
|
|
/* flags = */ 0, nullptr);
|
|
|
|
if (!func) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, JS_GetFunctionObject(func));
|
|
|
|
|
2019-05-03 13:09:11 +03:00
|
|
|
JS::AssertObjectIsNotGray(aHolder);
|
2016-02-10 01:40:31 +03:00
|
|
|
js::SetFunctionNativeReserved(obj, SLOT_NATIVEHANDLER,
|
|
|
|
JS::ObjectValue(*aHolder));
|
|
|
|
js::SetFunctionNativeReserved(obj, SLOT_NATIVEHANDLER_TASK,
|
|
|
|
JS::Int32Value(static_cast<int32_t>(aTask)));
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2016-09-07 00:38:13 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class PromiseNativeHandlerShim final : public PromiseNativeHandler {
|
|
|
|
RefPtr<PromiseNativeHandler> mInner;
|
|
|
|
|
|
|
|
~PromiseNativeHandlerShim() {}
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit PromiseNativeHandlerShim(PromiseNativeHandler* aInner)
|
|
|
|
: mInner(aInner) {
|
|
|
|
MOZ_ASSERT(mInner);
|
|
|
|
}
|
|
|
|
|
2019-03-19 18:53:43 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT
|
2016-09-07 00:38:13 +03:00
|
|
|
void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override {
|
2019-03-19 18:53:43 +03:00
|
|
|
RefPtr<PromiseNativeHandler> inner = mInner.forget();
|
|
|
|
inner->ResolvedCallback(aCx, aValue);
|
|
|
|
MOZ_ASSERT(!mInner);
|
2016-09-07 00:38:13 +03:00
|
|
|
}
|
|
|
|
|
2019-03-19 18:53:43 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT
|
2016-09-07 00:38:13 +03:00
|
|
|
void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override {
|
2019-03-19 18:53:43 +03:00
|
|
|
RefPtr<PromiseNativeHandler> inner = mInner.forget();
|
|
|
|
inner->RejectedCallback(aCx, aValue);
|
|
|
|
MOZ_ASSERT(!mInner);
|
2016-09-07 00:38:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto,
|
|
|
|
JS::MutableHandle<JSObject*> aWrapper) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return PromiseNativeHandler_Binding::Wrap(aCx, this, aGivenProto, aWrapper);
|
2016-09-07 00:38:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS(PromiseNativeHandlerShim)
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION(PromiseNativeHandlerShim, mInner)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(PromiseNativeHandlerShim)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(PromiseNativeHandlerShim)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PromiseNativeHandlerShim)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
void Promise::AppendNativeHandler(PromiseNativeHandler* aRunnable) {
|
2016-04-11 18:16:26 +03:00
|
|
|
NS_ASSERT_OWNINGTHREAD(Promise);
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(mGlobal))) {
|
|
|
|
// Our API doesn't allow us to return a useful error. Not like this should
|
|
|
|
// happen anyway.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-07 00:38:13 +03:00
|
|
|
// The self-hosted promise js may keep the object we pass to it alive
|
|
|
|
// for quite a while depending on when GC runs. Therefore, pass a shim
|
|
|
|
// object instead. The shim will free its inner PromiseNativeHandler
|
|
|
|
// after the promise has settled just like our previous c++ promises did.
|
|
|
|
RefPtr<PromiseNativeHandlerShim> shim =
|
|
|
|
new PromiseNativeHandlerShim(aRunnable);
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
JS::Rooted<JSObject*> handlerWrapper(cx);
|
|
|
|
// Note: PromiseNativeHandler is NOT wrappercached. So we can't use
|
|
|
|
// ToJSValue here, because it will try to do XPConnect wrapping on it, sadly.
|
2016-09-07 00:38:13 +03:00
|
|
|
if (NS_WARN_IF(!shim->WrapObject(cx, nullptr, &handlerWrapper))) {
|
2016-02-10 01:40:31 +03:00
|
|
|
// Again, no way to report errors.
|
|
|
|
jsapi.ClearException();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> resolveFunc(cx);
|
|
|
|
resolveFunc = CreateNativeHandlerFunction(cx, handlerWrapper,
|
|
|
|
NativeHandlerTask::Resolve);
|
|
|
|
if (NS_WARN_IF(!resolveFunc)) {
|
|
|
|
jsapi.ClearException();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> rejectFunc(cx);
|
|
|
|
rejectFunc = CreateNativeHandlerFunction(cx, handlerWrapper,
|
|
|
|
NativeHandlerTask::Reject);
|
|
|
|
if (NS_WARN_IF(!rejectFunc)) {
|
|
|
|
jsapi.ClearException();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
JS::Rooted<JSObject*> promiseObj(cx, PromiseObj());
|
2016-02-10 01:40:31 +03:00
|
|
|
if (NS_WARN_IF(
|
|
|
|
!JS::AddPromiseReactions(cx, promiseObj, resolveFunc, rejectFunc))) {
|
|
|
|
jsapi.ClearException();
|
|
|
|
return;
|
|
|
|
}
|
2016-02-10 01:40:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Promise::HandleException(JSContext* aCx) {
|
|
|
|
JS::Rooted<JS::Value> exn(aCx);
|
|
|
|
if (JS_GetPendingException(aCx, &exn)) {
|
|
|
|
JS_ClearPendingException(aCx);
|
2019-07-29 17:43:54 +03:00
|
|
|
// Always reject even if this was called in *Resolve.
|
2016-02-10 01:40:31 +03:00
|
|
|
MaybeReject(aCx, exn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<Promise> Promise::CreateFromExisting(
|
2018-10-10 00:42:22 +03:00
|
|
|
nsIGlobalObject* aGlobal, JS::Handle<JSObject*> aPromiseObj,
|
|
|
|
PropagateUserInteraction aPropagateUserInteraction) {
|
2019-05-03 18:15:31 +03:00
|
|
|
MOZ_ASSERT(
|
|
|
|
js::GetObjectCompartment(aGlobal->GetGlobalJSObjectPreserveColor()) ==
|
|
|
|
js::GetObjectCompartment(aPromiseObj));
|
2016-02-10 01:40:31 +03:00
|
|
|
RefPtr<Promise> p = new Promise(aGlobal);
|
2016-02-10 01:40:31 +03:00
|
|
|
p->mPromiseObj = aPromiseObj;
|
2018-10-10 00:42:22 +03:00
|
|
|
if (aPropagateUserInteraction == ePropagateUserInteraction &&
|
|
|
|
!p->MaybePropagateUserInputEventHandling()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-02-10 01:40:31 +03:00
|
|
|
return p.forget();
|
|
|
|
}
|
|
|
|
|
2016-08-10 05:07:42 +03:00
|
|
|
void Promise::MaybeResolveWithUndefined() {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Promise);
|
|
|
|
|
|
|
|
MaybeResolve(JS::UndefinedHandleValue);
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
void Promise::MaybeReject(const RefPtr<MediaStreamError>& aArg) {
|
2016-04-11 18:16:26 +03:00
|
|
|
NS_ASSERT_OWNINGTHREAD(Promise);
|
|
|
|
|
2014-09-20 10:20:41 +04:00
|
|
|
MaybeSomething(aArg, &Promise::MaybeReject);
|
|
|
|
}
|
|
|
|
|
2016-06-09 22:26:47 +03:00
|
|
|
void Promise::MaybeRejectWithUndefined() {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Promise);
|
|
|
|
|
|
|
|
MaybeSomething(JS::UndefinedHandleValue, &Promise::MaybeReject);
|
2016-01-19 09:35:01 +03:00
|
|
|
}
|
|
|
|
|
2016-03-22 18:22:24 +03:00
|
|
|
void Promise::ReportRejectedPromise(JSContext* aCx, JS::HandleObject aPromise) {
|
|
|
|
MOZ_ASSERT(!js::IsWrapper(aPromise));
|
|
|
|
|
|
|
|
MOZ_ASSERT(JS::GetPromiseState(aPromise) == JS::PromiseState::Rejected);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> result(aCx, JS::GetPromiseResult(aPromise));
|
|
|
|
|
|
|
|
RefPtr<xpc::ErrorReport> xpcReport = new xpc::ErrorReport();
|
|
|
|
bool isMainThread = MOZ_LIKELY(NS_IsMainThread());
|
|
|
|
bool isChrome = isMainThread ? nsContentUtils::IsSystemPrincipal(
|
|
|
|
nsContentUtils::ObjectPrincipal(aPromise))
|
2018-03-14 17:26:33 +03:00
|
|
|
: IsCurrentThreadRunningChromeWorker();
|
2017-11-04 01:25:38 +03:00
|
|
|
nsGlobalWindowInner* win =
|
|
|
|
isMainThread ? xpc::WindowGlobalOrNull(aPromise) : nullptr;
|
2018-08-24 00:01:04 +03:00
|
|
|
|
|
|
|
js::ErrorReport report(aCx);
|
2019-06-12 00:53:27 +03:00
|
|
|
RefPtr<Exception> exn;
|
|
|
|
if (result.isObject() &&
|
|
|
|
(NS_SUCCEEDED(UNWRAP_OBJECT(DOMException, &result, exn)) ||
|
|
|
|
NS_SUCCEEDED(UNWRAP_OBJECT(Exception, &result, exn)))) {
|
|
|
|
xpcReport->Init(aCx, exn, isChrome, win ? win->WindowID() : 0);
|
|
|
|
} else if (report.init(aCx, result, js::ErrorReport::NoSideEffects)) {
|
2018-08-24 00:01:04 +03:00
|
|
|
xpcReport->Init(report.report(), report.toStringResult().c_str(), isChrome,
|
2019-04-11 17:12:43 +03:00
|
|
|
win ? win->WindowID() : 0);
|
2018-08-24 00:01:04 +03:00
|
|
|
} else {
|
|
|
|
JS_ClearPendingException(aCx);
|
2019-06-12 00:53:27 +03:00
|
|
|
return;
|
2018-08-24 00:01:04 +03:00
|
|
|
}
|
2016-03-22 18:22:24 +03:00
|
|
|
|
|
|
|
// Now post an event to do the real reporting async
|
2018-02-20 17:52:32 +03:00
|
|
|
RefPtr<nsIRunnable> event = new AsyncErrorReporter(xpcReport);
|
|
|
|
if (win) {
|
|
|
|
win->Dispatch(mozilla::TaskCategory::Other, event.forget());
|
|
|
|
} else {
|
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
}
|
2016-03-22 18:22:24 +03:00
|
|
|
}
|
|
|
|
|
2019-03-26 22:34:29 +03:00
|
|
|
void Promise::MaybeResolveWithClone(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) {
|
|
|
|
JS::Rooted<JSObject*> sourceScope(aCx, JS::CurrentGlobalOrNull(aCx));
|
|
|
|
AutoEntryScript aes(GetParentObject(), "Promise resolution");
|
|
|
|
JSContext* cx = aes.cx();
|
|
|
|
JS::Rooted<JS::Value> value(cx, aValue);
|
|
|
|
|
|
|
|
xpc::StackScopedCloneOptions options;
|
|
|
|
options.wrapReflectors = true;
|
2019-07-29 17:43:54 +03:00
|
|
|
if (!StackScopedClone(cx, options, sourceScope, &value)) {
|
|
|
|
HandleException(cx);
|
|
|
|
return;
|
|
|
|
}
|
2019-03-26 22:34:29 +03:00
|
|
|
MaybeResolve(aCx, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Promise::MaybeRejectWithClone(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) {
|
|
|
|
JS::Rooted<JSObject*> sourceScope(aCx, JS::CurrentGlobalOrNull(aCx));
|
|
|
|
AutoEntryScript aes(GetParentObject(), "Promise rejection");
|
|
|
|
JSContext* cx = aes.cx();
|
|
|
|
JS::Rooted<JS::Value> value(cx, aValue);
|
|
|
|
|
|
|
|
xpc::StackScopedCloneOptions options;
|
|
|
|
options.wrapReflectors = true;
|
2019-07-29 17:43:54 +03:00
|
|
|
if (!StackScopedClone(cx, options, sourceScope, &value)) {
|
|
|
|
HandleException(cx);
|
|
|
|
return;
|
|
|
|
}
|
2019-03-26 22:34:29 +03:00
|
|
|
MaybeReject(aCx, value);
|
|
|
|
}
|
|
|
|
|
2016-12-11 20:12:01 +03:00
|
|
|
// A WorkerRunnable to resolve/reject the Promise on the worker thread.
|
|
|
|
// Calling thread MUST hold PromiseWorkerProxy's mutex before creating this.
|
|
|
|
class PromiseWorkerProxyRunnable : public WorkerRunnable {
|
|
|
|
public:
|
|
|
|
PromiseWorkerProxyRunnable(PromiseWorkerProxy* aPromiseWorkerProxy,
|
|
|
|
PromiseWorkerProxy::RunCallbackFunc aFunc)
|
|
|
|
: WorkerRunnable(aPromiseWorkerProxy->GetWorkerPrivate(),
|
|
|
|
WorkerThreadUnchangedBusyCount),
|
|
|
|
mPromiseWorkerProxy(aPromiseWorkerProxy),
|
|
|
|
mFunc(aFunc) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mPromiseWorkerProxy);
|
2013-09-11 20:03:04 +04:00
|
|
|
}
|
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
virtual bool WorkerRun(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate) override {
|
2016-12-11 20:12:01 +03:00
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(aWorkerPrivate == mWorkerPrivate);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mPromiseWorkerProxy);
|
|
|
|
RefPtr<Promise> workerPromise = mPromiseWorkerProxy->WorkerPromise();
|
2013-09-11 20:03:04 +04:00
|
|
|
|
2016-12-11 20:12:01 +03:00
|
|
|
// Here we convert the buffer to a JS::Value.
|
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
|
|
|
if (!mPromiseWorkerProxy->Read(aCx, &value)) {
|
|
|
|
JS_ClearPendingException(aCx);
|
2014-10-20 06:27:12 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-09-11 20:03:04 +04:00
|
|
|
|
2016-12-11 20:12:01 +03:00
|
|
|
(workerPromise->*mFunc)(aCx, value);
|
2013-09-11 20:03:04 +04:00
|
|
|
|
2016-12-11 20:12:01 +03:00
|
|
|
// Release the Promise because it has been resolved/rejected for sure.
|
|
|
|
mPromiseWorkerProxy->CleanUp();
|
2014-01-23 22:47:29 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-11 20:12:01 +03:00
|
|
|
protected:
|
|
|
|
~PromiseWorkerProxyRunnable() {}
|
2015-01-13 06:35:33 +03:00
|
|
|
|
2016-12-11 20:12:01 +03:00
|
|
|
private:
|
|
|
|
RefPtr<PromiseWorkerProxy> mPromiseWorkerProxy;
|
2014-01-23 22:47:29 +04:00
|
|
|
|
2016-12-11 20:12:01 +03:00
|
|
|
// Function pointer for calling Promise::{ResolveInternal,RejectInternal}.
|
|
|
|
PromiseWorkerProxy::RunCallbackFunc mFunc;
|
|
|
|
};
|
2014-01-23 22:47:29 +04:00
|
|
|
|
2014-12-17 17:49:36 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<PromiseWorkerProxy> PromiseWorkerProxy::Create(
|
2016-07-17 17:48:58 +03:00
|
|
|
WorkerPrivate* aWorkerPrivate, Promise* aWorkerPromise,
|
2015-09-05 12:22:13 +03:00
|
|
|
const PromiseWorkerProxyStructuredCloneCallbacks* aCb) {
|
2014-12-17 17:49:36 +03:00
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(aWorkerPromise);
|
2015-09-05 12:22:13 +03:00
|
|
|
MOZ_ASSERT_IF(aCb, !!aCb->Write && !!aCb->Read);
|
2014-12-17 17:49:36 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> proxy =
|
2018-03-14 17:26:33 +03:00
|
|
|
new PromiseWorkerProxy(aWorkerPromise, aCb);
|
2014-12-17 17:49:36 +03:00
|
|
|
|
|
|
|
// We do this to make sure the worker thread won't shut down before the
|
|
|
|
// promise is resolved/rejected on the worker thread.
|
2018-03-14 17:26:33 +03:00
|
|
|
RefPtr<StrongWorkerRef> workerRef = StrongWorkerRef::Create(
|
|
|
|
aWorkerPrivate, "PromiseWorkerProxy", [proxy]() { proxy->CleanUp(); });
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!workerRef)) {
|
2014-12-17 17:49:36 +03:00
|
|
|
// Probably the worker is terminating. We cannot complete the operation
|
|
|
|
// and we have to release all the resources.
|
2015-09-02 20:07:26 +03:00
|
|
|
proxy->CleanProperties();
|
2014-12-17 17:49:36 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-03-14 17:26:33 +03:00
|
|
|
proxy->mWorkerRef = new ThreadSafeWorkerRef(workerRef);
|
|
|
|
|
|
|
|
// Maintain a reference so that we have a valid object to clean up when
|
|
|
|
// removing the feature.
|
|
|
|
proxy.get()->AddRef();
|
|
|
|
|
2014-12-17 17:49:36 +03:00
|
|
|
return proxy.forget();
|
|
|
|
}
|
|
|
|
|
2015-07-09 09:56:00 +03:00
|
|
|
NS_IMPL_ISUPPORTS0(PromiseWorkerProxy)
|
|
|
|
|
2018-03-14 17:26:33 +03:00
|
|
|
PromiseWorkerProxy::PromiseWorkerProxy(
|
|
|
|
Promise* aWorkerPromise,
|
2015-09-05 12:22:13 +03:00
|
|
|
const PromiseWorkerProxyStructuredCloneCallbacks* aCallbacks)
|
2018-03-14 17:26:33 +03:00
|
|
|
: mWorkerPromise(aWorkerPromise),
|
2014-02-24 17:56:54 +04:00
|
|
|
mCleanedUp(false),
|
|
|
|
mCallbacks(aCallbacks),
|
|
|
|
mCleanUpLock("cleanUpLock") {}
|
|
|
|
|
|
|
|
PromiseWorkerProxy::~PromiseWorkerProxy() {
|
|
|
|
MOZ_ASSERT(mCleanedUp);
|
|
|
|
MOZ_ASSERT(!mWorkerPromise);
|
2018-03-14 17:26:33 +03:00
|
|
|
MOZ_ASSERT(!mWorkerRef);
|
2015-09-02 20:07:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PromiseWorkerProxy::CleanProperties() {
|
2018-03-14 17:25:47 +03:00
|
|
|
MOZ_ASSERT(IsCurrentThreadRunningWorker());
|
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
// Ok to do this unprotected from Create().
|
|
|
|
// CleanUp() holds the lock before calling this.
|
|
|
|
mCleanedUp = true;
|
|
|
|
mWorkerPromise = nullptr;
|
2018-03-14 17:26:33 +03:00
|
|
|
mWorkerRef = nullptr;
|
2015-09-05 12:22:13 +03:00
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
// Clear the StructuredCloneHolderBase class.
|
|
|
|
Clear();
|
2015-09-02 20:07:26 +03:00
|
|
|
}
|
|
|
|
|
2014-02-24 17:56:54 +04:00
|
|
|
WorkerPrivate* PromiseWorkerProxy::GetWorkerPrivate() const {
|
2015-06-11 01:35:18 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
mCleanUpLock.AssertCurrentThreadOwns();
|
|
|
|
}
|
|
|
|
#endif
|
2015-09-02 20:07:26 +03:00
|
|
|
// Safe to check this without a lock since we assert lock ownership on the
|
|
|
|
// main thread above.
|
|
|
|
MOZ_ASSERT(!mCleanedUp);
|
2018-03-14 17:26:33 +03:00
|
|
|
MOZ_ASSERT(mWorkerRef);
|
2015-06-11 01:35:18 +03:00
|
|
|
|
2018-03-14 17:26:33 +03:00
|
|
|
return mWorkerRef->Private();
|
2014-02-24 17:56:54 +04:00
|
|
|
}
|
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
Promise* PromiseWorkerProxy::WorkerPromise() const {
|
2018-03-14 17:25:47 +03:00
|
|
|
MOZ_ASSERT(IsCurrentThreadRunningWorker());
|
2015-09-02 20:07:26 +03:00
|
|
|
MOZ_ASSERT(mWorkerPromise);
|
2014-02-24 17:56:54 +04:00
|
|
|
return mWorkerPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PromiseWorkerProxy::RunCallback(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
RunCallbackFunc aFunc) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
MutexAutoLock lock(Lock());
|
2014-02-24 17:56:54 +04:00
|
|
|
// If the worker thread's been cancelled we don't need to resolve the Promise.
|
2015-09-02 20:07:26 +03:00
|
|
|
if (CleanedUp()) {
|
2014-02-24 17:56:54 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
// The |aValue| is written into the StructuredCloneHolderBase.
|
2015-09-05 12:22:13 +03:00
|
|
|
if (!Write(aCx, aValue)) {
|
2014-02-24 17:56:54 +04:00
|
|
|
JS_ClearPendingException(aCx);
|
2015-09-05 12:22:13 +03:00
|
|
|
MOZ_ASSERT(false,
|
|
|
|
"cannot serialize the value with the StructuredCloneAlgorithm!");
|
2014-02-24 17:56:54 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxyRunnable> runnable =
|
2015-09-05 12:22:13 +03:00
|
|
|
new PromiseWorkerProxyRunnable(this, aFunc);
|
2014-02-24 17:56:54 +04:00
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
runnable->Dispatch();
|
2014-02-24 17:56:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void PromiseWorkerProxy::ResolvedCallback(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) {
|
2016-02-10 01:40:31 +03:00
|
|
|
RunCallback(aCx, aValue, &Promise::MaybeResolve);
|
2014-02-24 17:56:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void PromiseWorkerProxy::RejectedCallback(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) {
|
2016-02-10 01:40:31 +03:00
|
|
|
RunCallback(aCx, aValue, &Promise::MaybeReject);
|
2014-02-24 17:56:54 +04:00
|
|
|
}
|
|
|
|
|
2016-02-29 22:52:42 +03:00
|
|
|
void PromiseWorkerProxy::CleanUp() {
|
2015-09-02 20:07:26 +03:00
|
|
|
// Can't release Mutex while it is still locked, so scope the lock.
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(Lock());
|
2014-02-24 17:56:54 +04:00
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
if (CleanedUp()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-24 17:56:54 +04:00
|
|
|
|
2018-03-14 17:26:33 +03:00
|
|
|
MOZ_ASSERT(mWorkerRef);
|
|
|
|
mWorkerRef->Private()->AssertIsOnWorkerThread();
|
2014-02-24 17:56:54 +04:00
|
|
|
|
2016-07-18 10:14:14 +03:00
|
|
|
// Release the Promise and remove the PromiseWorkerProxy from the holders of
|
2015-09-02 20:07:26 +03:00
|
|
|
// the worker thread since the Promise has been resolved/rejected or the
|
|
|
|
// worker thread has been cancelled.
|
2018-03-14 17:26:33 +03:00
|
|
|
mWorkerRef = nullptr;
|
2016-07-18 10:14:14 +03:00
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
CleanProperties();
|
|
|
|
}
|
|
|
|
Release();
|
2014-02-24 17:56:54 +04:00
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
JSObject* PromiseWorkerProxy::CustomReadHandler(
|
|
|
|
JSContext* aCx, JSStructuredCloneReader* aReader, uint32_t aTag,
|
|
|
|
uint32_t aIndex) {
|
2015-09-05 12:22:13 +03:00
|
|
|
if (NS_WARN_IF(!mCallbacks)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mCallbacks->Read(aCx, aReader, this, aTag, aIndex);
|
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
bool PromiseWorkerProxy::CustomWriteHandler(JSContext* aCx,
|
|
|
|
JSStructuredCloneWriter* aWriter,
|
|
|
|
JS::Handle<JSObject*> aObj) {
|
2015-09-05 12:22:13 +03:00
|
|
|
if (NS_WARN_IF(!mCallbacks)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mCallbacks->Write(aCx, aWriter, this, aObj);
|
|
|
|
}
|
|
|
|
|
2014-06-03 19:38:38 +04:00
|
|
|
// Specializations of MaybeRejectBrokenly we actually support.
|
|
|
|
template <>
|
2017-08-06 20:47:00 +03:00
|
|
|
void Promise::MaybeRejectBrokenly(const RefPtr<DOMException>& aArg) {
|
2014-06-03 19:38:38 +04:00
|
|
|
MaybeSomething(aArg, &Promise::MaybeReject);
|
|
|
|
}
|
|
|
|
template <>
|
|
|
|
void Promise::MaybeRejectBrokenly(const nsAString& aArg) {
|
|
|
|
MaybeSomething(aArg, &Promise::MaybeReject);
|
|
|
|
}
|
|
|
|
|
2016-09-07 05:51:41 +03:00
|
|
|
Promise::PromiseState Promise::State() const {
|
|
|
|
JS::Rooted<JSObject*> p(RootingCx(), PromiseObj());
|
|
|
|
const JS::PromiseState state = JS::GetPromiseState(p);
|
|
|
|
|
|
|
|
if (state == JS::PromiseState::Fulfilled) {
|
|
|
|
return PromiseState::Resolved;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == JS::PromiseState::Rejected) {
|
|
|
|
return PromiseState::Rejected;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PromiseState::Pending;
|
|
|
|
}
|
|
|
|
|
2013-06-12 05:41:21 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|