michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "CryptoTask.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: CryptoTask::~CryptoTask() michael@0: { michael@0: MOZ_ASSERT(mReleasedNSSResources); michael@0: michael@0: nsNSSShutDownPreventionLock lock; michael@0: if (!isAlreadyShutDown()) { michael@0: shutdown(calledFromObject); michael@0: } michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: CryptoTask::Run() michael@0: { michael@0: if (!NS_IsMainThread()) { michael@0: nsNSSShutDownPreventionLock locker; michael@0: if (isAlreadyShutDown()) { michael@0: mRv = NS_ERROR_NOT_AVAILABLE; michael@0: } else { michael@0: mRv = CalculateResult(); michael@0: } michael@0: NS_DispatchToMainThread(this); michael@0: } else { michael@0: // back on the main thread michael@0: michael@0: // call ReleaseNSSResources now, before calling CallCallback, so that michael@0: // CryptoTasks have consistent behavior regardless of whether NSS is shut michael@0: // down between CalculateResult being called and CallCallback being called. michael@0: if (!mReleasedNSSResources) { michael@0: mReleasedNSSResources = true; michael@0: ReleaseNSSResources(); michael@0: } michael@0: michael@0: CallCallback(mRv); michael@0: michael@0: // Not all uses of CryptoTask use a transient thread michael@0: if (mThread) { michael@0: // Don't leak threads! michael@0: mThread->Shutdown(); // can't Shutdown from the thread itself, darn michael@0: // Don't null out mThread! michael@0: // See bug 999104. We must hold a ref to the thread across Dispatch() michael@0: // since the internal mThread ref could be released while processing michael@0: // the Dispatch(), and Dispatch/PutEvent itself doesn't hold a ref; it michael@0: // assumes the caller does. michael@0: } michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: CryptoTask::virtualDestroyNSSReference() michael@0: { michael@0: NS_ABORT_IF_FALSE(NS_IsMainThread(), michael@0: "virtualDestroyNSSReference called off the main thread"); michael@0: if (!mReleasedNSSResources) { michael@0: mReleasedNSSResources = true; michael@0: ReleaseNSSResources(); michael@0: } michael@0: } michael@0: michael@0: } // namespace mozilla