security/manager/ssl/src/CryptoTask.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/manager/ssl/src/CryptoTask.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,71 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "CryptoTask.h"
    1.11 +
    1.12 +namespace mozilla {
    1.13 +
    1.14 +CryptoTask::~CryptoTask()
    1.15 +{
    1.16 +  MOZ_ASSERT(mReleasedNSSResources);
    1.17 +
    1.18 +  nsNSSShutDownPreventionLock lock;
    1.19 +  if (!isAlreadyShutDown()) {
    1.20 +    shutdown(calledFromObject);
    1.21 +  }
    1.22 +}
    1.23 +
    1.24 +NS_IMETHODIMP
    1.25 +CryptoTask::Run()
    1.26 +{
    1.27 +  if (!NS_IsMainThread()) {
    1.28 +    nsNSSShutDownPreventionLock locker;
    1.29 +    if (isAlreadyShutDown()) {
    1.30 +      mRv = NS_ERROR_NOT_AVAILABLE;
    1.31 +    } else {
    1.32 +      mRv = CalculateResult();
    1.33 +    }
    1.34 +    NS_DispatchToMainThread(this);
    1.35 +  } else {
    1.36 +    // back on the main thread
    1.37 +
    1.38 +    // call ReleaseNSSResources now, before calling CallCallback, so that
    1.39 +    // CryptoTasks have consistent behavior regardless of whether NSS is shut
    1.40 +    // down between CalculateResult being called and CallCallback being called.
    1.41 +    if (!mReleasedNSSResources) {
    1.42 +      mReleasedNSSResources = true;
    1.43 +      ReleaseNSSResources();
    1.44 +    }
    1.45 +
    1.46 +    CallCallback(mRv);
    1.47 +
    1.48 +    // Not all uses of CryptoTask use a transient thread
    1.49 +    if (mThread) {
    1.50 +      // Don't leak threads!
    1.51 +      mThread->Shutdown(); // can't Shutdown from the thread itself, darn
    1.52 +      // Don't null out mThread!
    1.53 +      // See bug 999104.  We must hold a ref to the thread across Dispatch()
    1.54 +      // since the internal mThread ref could be released while processing
    1.55 +      // the Dispatch(), and Dispatch/PutEvent itself doesn't hold a ref; it
    1.56 +      // assumes the caller does.
    1.57 +    }
    1.58 +  }
    1.59 +
    1.60 +  return NS_OK;
    1.61 +}
    1.62 +
    1.63 +void
    1.64 +CryptoTask::virtualDestroyNSSReference()
    1.65 +{
    1.66 +  NS_ABORT_IF_FALSE(NS_IsMainThread(),
    1.67 +                    "virtualDestroyNSSReference called off the main thread");
    1.68 +  if (!mReleasedNSSResources) {
    1.69 +    mReleasedNSSResources = true;
    1.70 +    ReleaseNSSResources();
    1.71 +  }
    1.72 +}
    1.73 +
    1.74 +} // namespace mozilla

mercurial