Bug 1296316 - Convert nsNSSShutDownObject::CalledFromType to an enum class. r=mgoodwin

enum classes are in general safer than plain enums, and as such should be
preferred.

MozReview-Commit-ID: 1FK89SNhdk4

--HG--
extra : rebase_source : 764c4855026c02d8c9e33ca33637fec54ea5ca31
This commit is contained in:
Cykesiopka 2016-08-20 23:00:19 +08:00
parent c21f788993
commit a16f7b0f6a
30 changed files with 81 additions and 74 deletions

View File

@ -163,7 +163,7 @@ CryptoKey::~CryptoKey()
return;
}
destructorSafeDestroyNSSReference();
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
JSObject*

View File

@ -3724,7 +3724,7 @@ WebCryptoTask::~WebCryptoTask()
nsNSSShutDownPreventionLock lock;
if (!isAlreadyShutDown()) {
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
if (mWorkerHolder) {

View File

@ -299,7 +299,7 @@ RTCCertificate::~RTCCertificate()
return;
}
destructorSafeDestroyNSSReference();
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
// This creates some interesting lifecycle consequences, since the DtlsIdentity

View File

@ -104,7 +104,7 @@ U2FRegisterTask::~U2FRegisterTask()
if (isAlreadyShutDown()) {
return;
}
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
void
@ -307,7 +307,7 @@ U2FSignTask::~U2FSignTask()
if (isAlreadyShutDown()) {
return;
}
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
void
@ -555,7 +555,7 @@ U2F::~U2F()
if (isAlreadyShutDown()) {
return;
}
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
/* virtual */ JSObject*

View File

@ -440,7 +440,7 @@ WifiCertService::~WifiCertService()
if (isAlreadyShutDown()) {
return;
}
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
already_AddRefed<WifiCertService>

View File

@ -282,7 +282,7 @@ private:
if (isAlreadyShutDown()) {
return;
}
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
};
@ -693,7 +693,7 @@ KeyStore::~KeyStore()
return;
}
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
MOZ_ASSERT(!mListenSocket);
MOZ_ASSERT(!mStreamSocket);

View File

@ -121,7 +121,7 @@ BackgroundFileSaver::~BackgroundFileSaver()
return;
}
destructorSafeDestroyNSSReference();
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
void
@ -1209,7 +1209,7 @@ DigestOutputStream::~DigestOutputStream()
if (isAlreadyShutDown()) {
return;
}
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
NS_IMETHODIMP

View File

@ -49,7 +49,7 @@ ContentSignatureVerifier::~ContentSignatureVerifier()
return;
}
destructorSafeDestroyNSSReference();
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
NS_IMETHODIMP

View File

@ -15,7 +15,7 @@ CryptoTask::~CryptoTask()
nsNSSShutDownPreventionLock lock;
if (!isAlreadyShutDown()) {
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
}

View File

@ -38,7 +38,7 @@ SecretDecoderRing::~SecretDecoderRing()
return;
}
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
nsresult

View File

@ -54,7 +54,7 @@ TransportSecurityInfo::~TransportSecurityInfo()
if (isAlreadyShutDown())
return;
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
void

View File

@ -37,7 +37,7 @@ nsCertPicker::~nsCertPicker()
return;
}
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
NS_IMETHODIMP nsCertPicker::PickByUsage(nsIInterfaceRequestor *ctx,

View File

@ -30,7 +30,7 @@ nsPkcs11::~nsPkcs11()
if (isAlreadyShutDown()) {
return;
}
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
// Delete a PKCS11 module from the user's profile.

View File

@ -36,7 +36,7 @@ nsCryptoHash::~nsCryptoHash()
return;
}
destructorSafeDestroyNSSReference();
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
void
@ -238,7 +238,7 @@ nsCryptoHMAC::~nsCryptoHMAC()
return;
}
destructorSafeDestroyNSSReference();
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
void

View File

@ -44,7 +44,7 @@ nsDataSignatureVerifier::~nsDataSignatureVerifier()
return;
}
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
NS_IMETHODIMP

View File

@ -24,7 +24,7 @@ nsKeyObject::~nsKeyObject()
return;
}
destructorSafeDestroyNSSReference();
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
void
@ -105,7 +105,7 @@ nsKeyObjectFactory::~nsKeyObjectFactory()
if (isAlreadyShutDown()) {
return;
}
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
NS_IMETHODIMP

View File

@ -202,7 +202,7 @@ nsKeygenFormProcessor::~nsKeygenFormProcessor()
return;
}
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
nsresult

View File

@ -750,7 +750,7 @@ PK11PasswordPromptRunnable::~PK11PasswordPromptRunnable()
return;
}
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
void

View File

@ -37,7 +37,7 @@ nsX509CertValidity::~nsX509CertValidity()
return;
}
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
NS_IMETHODIMP

View File

@ -171,7 +171,7 @@ nsNSSCertificate::~nsNSSCertificate()
return;
}
destructorSafeDestroyNSSReference();
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
void nsNSSCertificate::virtualDestroyNSSReference()
@ -1448,7 +1448,7 @@ nsNSSCertList::~nsNSSCertList()
return;
}
destructorSafeDestroyNSSReference();
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
void nsNSSCertList::virtualDestroyNSSReference()
@ -1735,7 +1735,7 @@ nsNSSCertListEnumerator::~nsNSSCertListEnumerator()
return;
}
destructorSafeDestroyNSSReference();
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
void nsNSSCertListEnumerator::virtualDestroyNSSReference()

View File

@ -91,7 +91,7 @@ nsNSSCertificateDB::~nsNSSCertificateDB()
return;
}
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
NS_IMETHODIMP

View File

@ -2,8 +2,9 @@
* 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/. */
#include "mozilla/Casting.h"
#include "nsNSSShutDown.h"
#include "mozilla/Casting.h"
#include "nsCOMPtr.h"
using namespace mozilla;
@ -172,7 +173,7 @@ nsresult nsNSSShutDownList::evaporateAllNSSResources()
auto entry = static_cast<ObjectHashEntry*>(iter.Get());
{
StaticMutexAutoUnlock unlock(sListLock);
entry->obj->shutdown(nsNSSShutDownObject::calledFromList);
entry->obj->shutdown(nsNSSShutDownObject::ShutdownCalledFrom::List);
}
iter.Remove();
}

View File

@ -2,15 +2,16 @@
* 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/. */
#ifndef _INC_NSSShutDown_H
#define _INC_NSSShutDown_H
#ifndef nsNSSShutDown_h
#define nsNSSShutDown_h
#include "nscore.h"
#include "nspr.h"
#include "PLDHashTable.h"
#include "mozilla/Assertions.h"
#include "mozilla/CondVar.h"
#include "mozilla/Mutex.h"
#include "mozilla/StaticMutex.h"
#include "nscore.h"
#include "nspr.h"
class nsNSSShutDownObject;
class nsOnPK11LogoutCancelObject;
@ -29,7 +30,7 @@ public:
// Wait for all activity to stop, and block any other thread on entering
// relevant PSM code.
PRStatus restrictActivityToCurrentThread();
// Go back to normal state.
void releaseCurrentThreadActivityRestriction();
@ -101,10 +102,10 @@ protected:
A class deriving from nsNSSShutDownObject will have its instances
automatically tracked in a list. However, it must follow some rules
to assure correct behaviour.
The tricky part is that it is not possible to call virtual
functions from a destructor.
The deriving class must override virtualDestroyNSSReference().
Within this function, it should clean up all resources held to NSS.
The function will be called by the global list, if it is time to
@ -124,7 +125,7 @@ protected:
then check to see if NSS has already been shut down by calling
isAlreadyShutDown(). If NSS has not been shut down, the destructor
must then call destructorSafeDestroyNSSReference() and then
shutdown(calledFromObject). The second call will deregister with
shutdown(ShutdownCalledFrom::Object). The second call will deregister with
the tracking list, to ensure no additional attempt to free the resources
will be made.
@ -163,7 +164,7 @@ protected:
{
destructorSafeDestroyNSSReference();
}
void destructorSafeDestroyNSSReference()
{
// clean up all NSS resources here
@ -176,14 +177,14 @@ protected:
return;
}
destructorSafeDestroyNSSReference();
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
NS_IMETHODIMP doSomething()
{
if (isAlreadyShutDown())
return NS_ERROR_NOT_AVAILABLE;
// use the NSS resources and do something
}
};
@ -192,35 +193,41 @@ protected:
class nsNSSShutDownObject
{
public:
enum CalledFromType {calledFromList, calledFromObject};
enum class ShutdownCalledFrom {
List,
Object,
};
nsNSSShutDownObject()
{
mAlreadyShutDown = false;
nsNSSShutDownList::remember(this);
}
virtual ~nsNSSShutDownObject()
{
// the derived class must call
// shutdown(calledFromObject);
// The derived class must call
// shutdown(ShutdownCalledFrom::Object);
// in its destructor
}
void shutdown(CalledFromType calledFrom)
void shutdown(ShutdownCalledFrom calledFrom)
{
if (!mAlreadyShutDown) {
if (calledFromObject == calledFrom) {
nsNSSShutDownList::forget(this);
}
if (calledFromList == calledFrom) {
virtualDestroyNSSReference();
switch (calledFrom) {
case ShutdownCalledFrom::Object:
nsNSSShutDownList::forget(this);
break;
case ShutdownCalledFrom::List:
virtualDestroyNSSReference();
break;
default:
MOZ_CRASH("shutdown() called from an unknown source");
}
mAlreadyShutDown = true;
}
}
bool isAlreadyShutDown() const { return mAlreadyShutDown; }
protected:
@ -233,26 +240,25 @@ class nsOnPK11LogoutCancelObject
{
public:
nsOnPK11LogoutCancelObject()
:mIsLoggedOut(false)
: mIsLoggedOut(false)
{
nsNSSShutDownList::remember(this);
}
virtual ~nsOnPK11LogoutCancelObject()
{
nsNSSShutDownList::forget(this);
}
void logout()
{
// We do not care for a race condition.
// Once the bool arrived at false,
// later calls to isPK11LoggedOut() will see it.
// This is a one-time change from 0 to 1.
mIsLoggedOut = true;
}
bool isPK11LoggedOut()
{
return mIsLoggedOut;
@ -262,4 +268,4 @@ private:
volatile bool mIsLoggedOut;
};
#endif
#endif // nsNSSShutDown_h

View File

@ -69,7 +69,7 @@ nsNSSU2FToken::~nsNSSU2FToken()
}
destructorSafeDestroyNSSReference();
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
void

View File

@ -92,7 +92,7 @@ nsPK11Token::~nsPK11Token()
return;
}
destructorSafeDestroyNSSReference();
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
void
@ -500,7 +500,7 @@ nsPK11TokenDB::~nsPK11TokenDB()
return;
}
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
NS_IMETHODIMP

View File

@ -81,7 +81,7 @@ nsPKCS11Slot::~nsPKCS11Slot()
return;
}
destructorSafeDestroyNSSReference();
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
void
@ -292,7 +292,7 @@ nsPKCS11Module::~nsPKCS11Module()
return;
}
destructorSafeDestroyNSSReference();
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
void
@ -423,7 +423,7 @@ nsPKCS11ModuleDB::~nsPKCS11ModuleDB()
return;
}
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
NS_IMETHODIMP

View File

@ -62,7 +62,7 @@ nsPKCS12Blob::~nsPKCS12Blob()
return;
}
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
// nsPKCS12Blob::SetToken

View File

@ -51,5 +51,5 @@ nsRandomGenerator::~nsRandomGenerator()
if (isAlreadyShutDown()) {
return;
}
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}

View File

@ -447,7 +447,7 @@ nsSyncJPAKE::~nsSyncJPAKE()
return;
}
destructorSafeDestroyNSSReference();
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
void

View File

@ -63,7 +63,7 @@ private:
return;
}
destructorSafeDestroyNSSReference();
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
void virtualDestroyNSSReference() override
@ -103,7 +103,7 @@ private:
return;
}
destructorSafeDestroyNSSReference();
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
virtual void virtualDestroyNSSReference() override
@ -140,7 +140,7 @@ private:
return;
}
destructorSafeDestroyNSSReference();
shutdown(calledFromObject);
shutdown(ShutdownCalledFrom::Object);
}
void virtualDestroyNSSReference() override