mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-11 04:15:43 +00:00
Bug 1058695 - Add member to nsIGlobalObject to detect it is going away. Make promises use it. r=bholley
--HG-- extra : rebase_source : 3b3909b843efa2150edfcd79a6399535a12bd5aa
This commit is contained in:
parent
34d521e285
commit
6e7fc932d3
@ -1439,6 +1439,8 @@ nsGlobalWindow::CleanUp()
|
|||||||
return;
|
return;
|
||||||
mCleanedUp = true;
|
mCleanedUp = true;
|
||||||
|
|
||||||
|
StartDying();
|
||||||
|
|
||||||
mEventTargetObjects.EnumerateEntries(DisconnectEventTargetObjects, nullptr);
|
mEventTargetObjects.EnumerateEntries(DisconnectEventTargetObjects, nullptr);
|
||||||
mEventTargetObjects.Clear();
|
mEventTargetObjects.Clear();
|
||||||
|
|
||||||
|
@ -17,13 +17,47 @@ class nsIPrincipal;
|
|||||||
|
|
||||||
class nsIGlobalObject : public nsISupports
|
class nsIGlobalObject : public nsISupports
|
||||||
{
|
{
|
||||||
|
bool mIsDying;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
nsIGlobalObject()
|
||||||
|
: mIsDying(false)
|
||||||
|
{}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
NS_DECLARE_STATIC_IID_ACCESSOR(NS_IGLOBALOBJECT_IID)
|
NS_DECLARE_STATIC_IID_ACCESSOR(NS_IGLOBALOBJECT_IID)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This check is added to deal with Promise microtask queues. On the main
|
||||||
|
* thread, we do not impose restrictions about when script stops running or
|
||||||
|
* when runnables can no longer be dispatched to the main thread. This means
|
||||||
|
* it is possible for a Promise chain to keep resolving an infinite chain of
|
||||||
|
* promises, preventing the browser from shutting down. See Bug 1058695. To
|
||||||
|
* prevent this, the nsGlobalWindow subclass sets this flag when it is
|
||||||
|
* closed. The Promise implementation checks this and prohibits new runnables
|
||||||
|
* from being dispatched.
|
||||||
|
*
|
||||||
|
* We pair this with checks during processing the promise microtask queue
|
||||||
|
* that pops up the slow script dialog when the Promise queue is preventing
|
||||||
|
* a window from going away.
|
||||||
|
*/
|
||||||
|
bool
|
||||||
|
IsDying() const
|
||||||
|
{
|
||||||
|
return mIsDying;
|
||||||
|
}
|
||||||
|
|
||||||
virtual JSObject* GetGlobalJSObject() = 0;
|
virtual JSObject* GetGlobalJSObject() = 0;
|
||||||
|
|
||||||
// This method is not meant to be overridden.
|
// This method is not meant to be overridden.
|
||||||
nsIPrincipal* PrincipalOrNull();
|
nsIPrincipal* PrincipalOrNull();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void
|
||||||
|
StartDying()
|
||||||
|
{
|
||||||
|
mIsDying = true;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
NS_DEFINE_STATIC_IID_ACCESSOR(nsIGlobalObject,
|
NS_DEFINE_STATIC_IID_ACCESSOR(nsIGlobalObject,
|
||||||
|
@ -263,7 +263,7 @@ protected:
|
|||||||
// console though, for debugging.
|
// console though, for debugging.
|
||||||
}
|
}
|
||||||
|
|
||||||
return NS_OK;
|
return rv.ErrorCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -489,13 +489,24 @@ Promise::PerformMicroTaskCheckpoint()
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Maybe<AutoSafeJSContext> cx;
|
||||||
|
if (NS_IsMainThread()) {
|
||||||
|
cx.emplace();
|
||||||
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
nsCOMPtr<nsIRunnable> runnable = microtaskQueue.ElementAt(0);
|
nsCOMPtr<nsIRunnable> runnable = microtaskQueue.ElementAt(0);
|
||||||
MOZ_ASSERT(runnable);
|
MOZ_ASSERT(runnable);
|
||||||
|
|
||||||
// This function can re-enter, so we remove the element before calling.
|
// This function can re-enter, so we remove the element before calling.
|
||||||
microtaskQueue.RemoveElementAt(0);
|
microtaskQueue.RemoveElementAt(0);
|
||||||
runnable->Run();
|
nsresult rv = runnable->Run();
|
||||||
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (cx.isSome()) {
|
||||||
|
JS_CheckForInterrupt(cx.ref());
|
||||||
|
}
|
||||||
} while (!microtaskQueue.IsEmpty());
|
} while (!microtaskQueue.IsEmpty());
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -1071,6 +1082,10 @@ void
|
|||||||
Promise::AppendCallbacks(PromiseCallback* aResolveCallback,
|
Promise::AppendCallbacks(PromiseCallback* aResolveCallback,
|
||||||
PromiseCallback* aRejectCallback)
|
PromiseCallback* aRejectCallback)
|
||||||
{
|
{
|
||||||
|
if (mGlobal->IsDying()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
MOZ_ASSERT(aResolveCallback);
|
MOZ_ASSERT(aResolveCallback);
|
||||||
MOZ_ASSERT(aRejectCallback);
|
MOZ_ASSERT(aRejectCallback);
|
||||||
|
|
||||||
@ -1297,7 +1312,12 @@ Promise::RejectInternal(JSContext* aCx,
|
|||||||
void
|
void
|
||||||
Promise::Settle(JS::Handle<JS::Value> aValue, PromiseState aState)
|
Promise::Settle(JS::Handle<JS::Value> aValue, PromiseState aState)
|
||||||
{
|
{
|
||||||
|
if (mGlobal->IsDying()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
mSettlementTimestamp = TimeStamp::Now();
|
mSettlementTimestamp = TimeStamp::Now();
|
||||||
|
|
||||||
SetResult(aValue);
|
SetResult(aValue);
|
||||||
SetState(aState);
|
SetState(aState);
|
||||||
|
|
||||||
|
@ -71,7 +71,7 @@ ResolvePromiseCallback::~ResolvePromiseCallback()
|
|||||||
DropJSObjects(this);
|
DropJSObjects(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
nsresult
|
||||||
ResolvePromiseCallback::Call(JSContext* aCx,
|
ResolvePromiseCallback::Call(JSContext* aCx,
|
||||||
JS::Handle<JS::Value> aValue)
|
JS::Handle<JS::Value> aValue)
|
||||||
{
|
{
|
||||||
@ -81,10 +81,11 @@ ResolvePromiseCallback::Call(JSContext* aCx,
|
|||||||
JS::Rooted<JS::Value> value(aCx, aValue);
|
JS::Rooted<JS::Value> value(aCx, aValue);
|
||||||
if (!JS_WrapValue(aCx, &value)) {
|
if (!JS_WrapValue(aCx, &value)) {
|
||||||
NS_WARNING("Failed to wrap value into the right compartment.");
|
NS_WARNING("Failed to wrap value into the right compartment.");
|
||||||
return;
|
return NS_ERROR_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
mPromise->ResolveInternal(aCx, value);
|
mPromise->ResolveInternal(aCx, value);
|
||||||
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
// RejectPromiseCallback
|
// RejectPromiseCallback
|
||||||
@ -129,7 +130,7 @@ RejectPromiseCallback::~RejectPromiseCallback()
|
|||||||
DropJSObjects(this);
|
DropJSObjects(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
nsresult
|
||||||
RejectPromiseCallback::Call(JSContext* aCx,
|
RejectPromiseCallback::Call(JSContext* aCx,
|
||||||
JS::Handle<JS::Value> aValue)
|
JS::Handle<JS::Value> aValue)
|
||||||
{
|
{
|
||||||
@ -139,11 +140,12 @@ RejectPromiseCallback::Call(JSContext* aCx,
|
|||||||
JS::Rooted<JS::Value> value(aCx, aValue);
|
JS::Rooted<JS::Value> value(aCx, aValue);
|
||||||
if (!JS_WrapValue(aCx, &value)) {
|
if (!JS_WrapValue(aCx, &value)) {
|
||||||
NS_WARNING("Failed to wrap value into the right compartment.");
|
NS_WARNING("Failed to wrap value into the right compartment.");
|
||||||
return;
|
return NS_ERROR_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
mPromise->RejectInternal(aCx, value);
|
mPromise->RejectInternal(aCx, value);
|
||||||
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
// WrapperPromiseCallback
|
// WrapperPromiseCallback
|
||||||
@ -190,7 +192,7 @@ WrapperPromiseCallback::~WrapperPromiseCallback()
|
|||||||
DropJSObjects(this);
|
DropJSObjects(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
nsresult
|
||||||
WrapperPromiseCallback::Call(JSContext* aCx,
|
WrapperPromiseCallback::Call(JSContext* aCx,
|
||||||
JS::Handle<JS::Value> aValue)
|
JS::Handle<JS::Value> aValue)
|
||||||
{
|
{
|
||||||
@ -198,7 +200,7 @@ WrapperPromiseCallback::Call(JSContext* aCx,
|
|||||||
JS::Rooted<JS::Value> value(aCx, aValue);
|
JS::Rooted<JS::Value> value(aCx, aValue);
|
||||||
if (!JS_WrapValue(aCx, &value)) {
|
if (!JS_WrapValue(aCx, &value)) {
|
||||||
NS_WARNING("Failed to wrap value into the right compartment.");
|
NS_WARNING("Failed to wrap value into the right compartment.");
|
||||||
return;
|
return NS_ERROR_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorResult rv;
|
ErrorResult rv;
|
||||||
@ -219,7 +221,7 @@ WrapperPromiseCallback::Call(JSContext* aCx,
|
|||||||
|
|
||||||
if (!JS_WrapValue(aCx, &value)) {
|
if (!JS_WrapValue(aCx, &value)) {
|
||||||
NS_WARNING("Failed to wrap value into the right compartment.");
|
NS_WARNING("Failed to wrap value into the right compartment.");
|
||||||
return;
|
return NS_ERROR_FAILURE;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Convert the ErrorResult to a JS exception object that we can reject
|
// Convert the ErrorResult to a JS exception object that we can reject
|
||||||
@ -232,7 +234,7 @@ WrapperPromiseCallback::Call(JSContext* aCx,
|
|||||||
}
|
}
|
||||||
|
|
||||||
mNextPromise->RejectInternal(aCx, value);
|
mNextPromise->RejectInternal(aCx, value);
|
||||||
return;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the return value is the same as the promise itself, throw TypeError.
|
// If the return value is the same as the promise itself, throw TypeError.
|
||||||
@ -270,7 +272,7 @@ WrapperPromiseCallback::Call(JSContext* aCx,
|
|||||||
if (!fn) {
|
if (!fn) {
|
||||||
// Out of memory. Promise will stay unresolved.
|
// Out of memory. Promise will stay unresolved.
|
||||||
JS_ClearPendingException(aCx);
|
JS_ClearPendingException(aCx);
|
||||||
return;
|
return NS_ERROR_OUT_OF_MEMORY;
|
||||||
}
|
}
|
||||||
|
|
||||||
JS::Rooted<JSString*> message(aCx,
|
JS::Rooted<JSString*> message(aCx,
|
||||||
@ -279,7 +281,7 @@ WrapperPromiseCallback::Call(JSContext* aCx,
|
|||||||
if (!message) {
|
if (!message) {
|
||||||
// Out of memory. Promise will stay unresolved.
|
// Out of memory. Promise will stay unresolved.
|
||||||
JS_ClearPendingException(aCx);
|
JS_ClearPendingException(aCx);
|
||||||
return;
|
return NS_ERROR_OUT_OF_MEMORY;
|
||||||
}
|
}
|
||||||
|
|
||||||
JS::Rooted<JS::Value> typeError(aCx);
|
JS::Rooted<JS::Value> typeError(aCx);
|
||||||
@ -287,21 +289,22 @@ WrapperPromiseCallback::Call(JSContext* aCx,
|
|||||||
nullptr, message, &typeError)) {
|
nullptr, message, &typeError)) {
|
||||||
// Out of memory. Promise will stay unresolved.
|
// Out of memory. Promise will stay unresolved.
|
||||||
JS_ClearPendingException(aCx);
|
JS_ClearPendingException(aCx);
|
||||||
return;
|
return NS_ERROR_OUT_OF_MEMORY;
|
||||||
}
|
}
|
||||||
|
|
||||||
mNextPromise->RejectInternal(aCx, typeError);
|
mNextPromise->RejectInternal(aCx, typeError);
|
||||||
return;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, run resolver's resolve with value.
|
// Otherwise, run resolver's resolve with value.
|
||||||
if (!JS_WrapValue(aCx, &retValue)) {
|
if (!JS_WrapValue(aCx, &retValue)) {
|
||||||
NS_WARNING("Failed to wrap value into the right compartment.");
|
NS_WARNING("Failed to wrap value into the right compartment.");
|
||||||
return;
|
return NS_ERROR_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
mNextPromise->ResolveInternal(aCx, retValue);
|
mNextPromise->ResolveInternal(aCx, retValue);
|
||||||
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
// NativePromiseCallback
|
// NativePromiseCallback
|
||||||
@ -327,21 +330,22 @@ NativePromiseCallback::~NativePromiseCallback()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
nsresult
|
||||||
NativePromiseCallback::Call(JSContext* aCx,
|
NativePromiseCallback::Call(JSContext* aCx,
|
||||||
JS::Handle<JS::Value> aValue)
|
JS::Handle<JS::Value> aValue)
|
||||||
{
|
{
|
||||||
if (mState == Promise::Resolved) {
|
if (mState == Promise::Resolved) {
|
||||||
mHandler->ResolvedCallback(aCx, aValue);
|
mHandler->ResolvedCallback(aCx, aValue);
|
||||||
return;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mState == Promise::Rejected) {
|
if (mState == Promise::Rejected) {
|
||||||
mHandler->RejectedCallback(aCx, aValue);
|
mHandler->RejectedCallback(aCx, aValue);
|
||||||
return;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
NS_NOTREACHED("huh?");
|
NS_NOTREACHED("huh?");
|
||||||
|
return NS_ERROR_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* static */ PromiseCallback*
|
/* static */ PromiseCallback*
|
||||||
|
@ -26,8 +26,8 @@ public:
|
|||||||
|
|
||||||
PromiseCallback();
|
PromiseCallback();
|
||||||
|
|
||||||
virtual void Call(JSContext* aCx,
|
virtual nsresult Call(JSContext* aCx,
|
||||||
JS::Handle<JS::Value> aValue) = 0;
|
JS::Handle<JS::Value> aValue) = 0;
|
||||||
|
|
||||||
// Return the Promise that this callback will end up resolving or
|
// Return the Promise that this callback will end up resolving or
|
||||||
// rejecting, if any.
|
// rejecting, if any.
|
||||||
@ -54,8 +54,8 @@ public:
|
|||||||
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(WrapperPromiseCallback,
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(WrapperPromiseCallback,
|
||||||
PromiseCallback)
|
PromiseCallback)
|
||||||
|
|
||||||
void Call(JSContext* aCx,
|
nsresult Call(JSContext* aCx,
|
||||||
JS::Handle<JS::Value> aValue) override;
|
JS::Handle<JS::Value> aValue) override;
|
||||||
|
|
||||||
Promise* GetDependentPromise() override
|
Promise* GetDependentPromise() override
|
||||||
{
|
{
|
||||||
@ -82,8 +82,8 @@ public:
|
|||||||
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(ResolvePromiseCallback,
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(ResolvePromiseCallback,
|
||||||
PromiseCallback)
|
PromiseCallback)
|
||||||
|
|
||||||
void Call(JSContext* aCx,
|
nsresult Call(JSContext* aCx,
|
||||||
JS::Handle<JS::Value> aValue) override;
|
JS::Handle<JS::Value> aValue) override;
|
||||||
|
|
||||||
Promise* GetDependentPromise() override
|
Promise* GetDependentPromise() override
|
||||||
{
|
{
|
||||||
@ -108,8 +108,8 @@ public:
|
|||||||
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(RejectPromiseCallback,
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(RejectPromiseCallback,
|
||||||
PromiseCallback)
|
PromiseCallback)
|
||||||
|
|
||||||
void Call(JSContext* aCx,
|
nsresult Call(JSContext* aCx,
|
||||||
JS::Handle<JS::Value> aValue) override;
|
JS::Handle<JS::Value> aValue) override;
|
||||||
|
|
||||||
Promise* GetDependentPromise() override
|
Promise* GetDependentPromise() override
|
||||||
{
|
{
|
||||||
@ -133,8 +133,8 @@ public:
|
|||||||
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(NativePromiseCallback,
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(NativePromiseCallback,
|
||||||
PromiseCallback)
|
PromiseCallback)
|
||||||
|
|
||||||
void Call(JSContext* aCx,
|
nsresult Call(JSContext* aCx,
|
||||||
JS::Handle<JS::Value> aValue) override;
|
JS::Handle<JS::Value> aValue) override;
|
||||||
|
|
||||||
Promise* GetDependentPromise() override
|
Promise* GetDependentPromise() override
|
||||||
{
|
{
|
||||||
|
@ -4462,6 +4462,12 @@ JS_New(JSContext* cx, HandleObject ctor, const JS::HandleValueArray& inputArgs)
|
|||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JS_PUBLIC_API(bool)
|
||||||
|
JS_CheckForInterrupt(JSContext* cx)
|
||||||
|
{
|
||||||
|
return js::CheckForInterrupt(cx);
|
||||||
|
}
|
||||||
|
|
||||||
JS_PUBLIC_API(JSInterruptCallback)
|
JS_PUBLIC_API(JSInterruptCallback)
|
||||||
JS_SetInterruptCallback(JSRuntime* rt, JSInterruptCallback callback)
|
JS_SetInterruptCallback(JSRuntime* rt, JSInterruptCallback callback)
|
||||||
{
|
{
|
||||||
|
@ -3948,9 +3948,11 @@ extern JS_PUBLIC_API(bool)
|
|||||||
Construct(JSContext* cx, JS::HandleValue fun,
|
Construct(JSContext* cx, JS::HandleValue fun,
|
||||||
const JS::HandleValueArray& args,
|
const JS::HandleValueArray& args,
|
||||||
MutableHandleValue rval);
|
MutableHandleValue rval);
|
||||||
|
|
||||||
} /* namespace JS */
|
} /* namespace JS */
|
||||||
|
|
||||||
|
extern JS_PUBLIC_API(bool)
|
||||||
|
JS_CheckForInterrupt(JSContext* cx);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* These functions allow setting an interrupt callback that will be called
|
* These functions allow setting an interrupt callback that will be called
|
||||||
* from the JS thread some time after any thread triggered the callback using
|
* from the JS thread some time after any thread triggered the callback using
|
||||||
|
@ -1461,8 +1461,13 @@ XPCJSRuntime::InterruptCallback(JSContext* cx)
|
|||||||
win = WindowGlobalOrNull(proto);
|
win = WindowGlobalOrNull(proto);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!win)
|
|
||||||
|
if (!win) {
|
||||||
|
NS_WARNING("No active window");
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
MOZ_ASSERT(!win->IsDying());
|
||||||
|
|
||||||
if (win->GetIsPrerendered()) {
|
if (win->GetIsPrerendered()) {
|
||||||
// We cannot display a dialog if the page is being prerendered, so
|
// We cannot display a dialog if the page is being prerendered, so
|
||||||
|
Loading…
Reference in New Issue
Block a user