From 697d925ff021c06b33039cb80c0584aac4ffa42d Mon Sep 17 00:00:00 2001 From: roytam1 Date: Fri, 29 Apr 2022 22:46:59 +0800 Subject: [PATCH] import from UXP: [network] BackgroundFileSaver cleanup. (562b04e5) --- netwerk/base/BackgroundFileSaver.cpp | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/netwerk/base/BackgroundFileSaver.cpp b/netwerk/base/BackgroundFileSaver.cpp index c3dbf73a8..d6905d0a9 100644 --- a/netwerk/base/BackgroundFileSaver.cpp +++ b/netwerk/base/BackgroundFileSaver.cpp @@ -253,7 +253,8 @@ BackgroundFileSaver::EnableSha256() nsresult rv; nsCOMPtr nssDummy = do_GetService("@mozilla.org/psm;1", &rv); NS_ENSURE_SUCCESS(rv, rv); - mSha256Enabled = true; + MutexAutoLock lock(mLock); + mSha256Enabled = true; // this will be read by the worker thread return NS_OK; } @@ -279,6 +280,7 @@ BackgroundFileSaver::EnableSignatureInfo() nsresult rv; nsCOMPtr nssDummy = do_GetService("@mozilla.org/psm;1", &rv); NS_ENSURE_SUCCESS(rv, rv); + MutexAutoLock lock(mLock); mSignatureInfoEnabled = true; return NS_OK; } @@ -392,9 +394,17 @@ BackgroundFileSaver::ProcessAttention() // If mAsyncCopyContext is not null, we interrupt the copy and re-enter // through AsyncCopyCallback. This allows us to check if, for instance, we // should rename the target file. We will then restart the copy if needed. - if (mAsyncCopyContext) { - NS_CancelAsyncCopy(mAsyncCopyContext, NS_ERROR_ABORT); - return NS_OK; + + // mAsyncCopyContext is only written on the worker thread (which we are on) + MOZ_ASSERT(!NS_IsMainThread()); + { + // Even though we're the only thread that writes this, we have to take the + // lock + MutexAutoLock lock(mLock); + if (mAsyncCopyContext) { + NS_CancelAsyncCopy(mAsyncCopyContext, NS_ERROR_ABORT); + return NS_OK; + } } // Use the current shared state to determine the next operation to execute. rv = ProcessStateChange(); @@ -673,12 +683,11 @@ BackgroundFileSaver::CheckCompletion() { nsresult rv; - MOZ_ASSERT(!mAsyncCopyContext, - "Should not be copying when checking completion conditions."); - bool failed = true; { MutexAutoLock lock(mLock); + MOZ_ASSERT(!mAsyncCopyContext, + "Should not be copying when checking completion conditions."); if (mComplete) { return true;