Bug 1342075 - Make nsCertOverride::OverrideBits an enum class. r?keeler draft
authorCykesiopka <cykesiopka.bmo@gmail.com>
Thu, 23 Feb 2017 23:16:53 +0800
changeset 488723 7856a81231033db783c86de264dc31cab89ec957
parent 488722 23e4839619c87a0866adb69df4b04acc2bb141d2
child 546797 5e88a5bdc6b04a0927b460b716f617003986c12a
push id46600
push usercykesiopka.bmo@gmail.com
push dateThu, 23 Feb 2017 15:18:47 +0000
reviewerskeeler
bugs1342075
milestone54.0a1
Bug 1342075 - Make nsCertOverride::OverrideBits an enum class. r?keeler MozReview-Commit-ID: 3aLyL9VDvpf
security/manager/ssl/nsCertOverrideService.cpp
security/manager/ssl/nsCertOverrideService.h
security/manager/ssl/nsCertTree.cpp
--- a/security/manager/ssl/nsCertOverrideService.cpp
+++ b/security/manager/ssl/nsCertOverrideService.cpp
@@ -18,67 +18,65 @@
 #include "nsIObserverService.h"
 #include "nsIOutputStream.h"
 #include "nsISafeOutputStream.h"
 #include "nsIX509Cert.h"
 #include "nsNSSCertHelper.h"
 #include "nsNSSCertificate.h"
 #include "nsNSSComponent.h"
 #include "nsNetUtil.h"
-#include "nsPromiseFlatString.h"
 #include "nsStreamUtils.h"
 #include "nsStringBuffer.h"
 #include "nsThreadUtils.h"
 #include "ssl.h" // For SSL_ClearSessionCache
 
 using namespace mozilla;
 using namespace mozilla::psm;
 
 #define CERT_OVERRIDE_FILE_NAME "cert_override.txt"
 
 void
-nsCertOverride::convertBitsToString(OverrideBits ob, nsACString &str)
+nsCertOverride::convertBitsToString(OverrideBits ob, /*out*/ nsACString& str)
 {
   str.Truncate();
 
-  if (ob & ob_Mismatch)
+  if (ob & OverrideBits::Mismatch) {
     str.Append('M');
+  }
 
-  if (ob & ob_Untrusted)
+  if (ob & OverrideBits::Untrusted) {
     str.Append('U');
+  }
 
-  if (ob & ob_Time_error)
+  if (ob & OverrideBits::Time) {
     str.Append('T');
+  }
 }
 
 void
-nsCertOverride::convertStringToBits(const nsACString &str, OverrideBits &ob)
+nsCertOverride::convertStringToBits(const nsACString& str,
+                            /*out*/ OverrideBits& ob)
 {
-  const nsPromiseFlatCString &flat = PromiseFlatCString(str);
-  const char *walk = flat.get();
+  ob = OverrideBits::None;
 
-  ob = ob_None;
-
-  for ( ; *walk; ++walk)
-  {
-    switch (*walk)
-    {
+  for (uint32_t i = 0; i < str.Length(); i++) {
+    switch (str.CharAt(i)) {
       case 'm':
       case 'M':
-        ob = (OverrideBits)(ob | ob_Mismatch);
+        ob |= OverrideBits::Mismatch;
         break;
 
       case 'u':
       case 'U':
-        ob = (OverrideBits)(ob | ob_Untrusted);
+        ob |= OverrideBits::Untrusted;
         break;
 
       case 't':
       case 'T':
-        ob = (OverrideBits)(ob | ob_Time_error);
+        ob |= OverrideBits::Time;
         break;
 
       default:
         break;
     }
   }
 }
 
@@ -454,33 +452,33 @@ nsCertOverrideService::HasMatchingOverri
   if (aPort < -1)
     return NS_ERROR_INVALID_ARG;
 
   NS_ENSURE_ARG_POINTER(aCert);
   NS_ENSURE_ARG_POINTER(aOverrideBits);
   NS_ENSURE_ARG_POINTER(aIsTemporary);
   NS_ENSURE_ARG_POINTER(_retval);
   *_retval = false;
-  *aOverrideBits = nsCertOverride::ob_None;
+  *aOverrideBits = static_cast<uint32_t>(nsCertOverride::OverrideBits::None);
 
   nsAutoCString hostPort;
   GetHostWithPort(aHostName, aPort, hostPort);
   nsCertOverride settings;
 
   {
     ReentrantMonitorAutoEnter lock(monitor);
     nsCertOverrideEntry *entry = mSettingsTable.GetEntry(hostPort.get());
 
     if (!entry)
       return NS_OK;
 
     settings = entry->mSettings; // copy
   }
 
-  *aOverrideBits = settings.mOverrideBits;
+  *aOverrideBits = static_cast<uint32_t>(settings.mOverrideBits);
   *aIsTemporary = settings.mIsTemporary;
 
   nsAutoCString fpStr;
   nsresult rv;
 
   // This code was originally written in a way that suggested that other hash
   // algorithms are supported for backwards compatibility. However, this was
   // always unnecessary, because only SHA256 has ever been used here.
@@ -504,34 +502,34 @@ nsCertOverrideService::GetValidityOverri
                                            uint32_t *aOverrideBits,
                                            bool *aIsTemporary,
                                            bool *_found)
 {
   NS_ENSURE_ARG_POINTER(_found);
   NS_ENSURE_ARG_POINTER(aIsTemporary);
   NS_ENSURE_ARG_POINTER(aOverrideBits);
   *_found = false;
-  *aOverrideBits = nsCertOverride::ob_None;
+  *aOverrideBits = static_cast<uint32_t>(nsCertOverride::OverrideBits::None);
 
   nsAutoCString hostPort;
   GetHostWithPort(aHostName, aPort, hostPort);
   nsCertOverride settings;
 
   {
     ReentrantMonitorAutoEnter lock(monitor);
     nsCertOverrideEntry *entry = mSettingsTable.GetEntry(hostPort.get());
 
     if (entry) {
       *_found = true;
       settings = entry->mSettings; // copy
     }
   }
 
   if (*_found) {
-    *aOverrideBits = settings.mOverrideBits;
+    *aOverrideBits = static_cast<uint32_t>(settings.mOverrideBits);
     *aIsTemporary = settings.mIsTemporary;
     aFingerprint = settings.mFingerprint;
     aHashAlg = settings.mFingerprintAlgOID;
   }
 
   return NS_OK;
 }
 
--- a/security/manager/ssl/nsCertOverrideService.h
+++ b/security/manager/ssl/nsCertOverrideService.h
@@ -1,37 +1,40 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  *
  * This Source Code Form is subject to the terms of the Mozilla Public
  * 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 __NSCERTOVERRIDESERVICE_H__
-#define __NSCERTOVERRIDESERVICE_H__
+#ifndef nsCertOverrideService_h
+#define nsCertOverrideService_h
 
 #include "mozilla/ReentrantMonitor.h"
+#include "mozilla/TypedEnumBits.h"
 #include "nsICertOverrideService.h"
-#include "nsTHashtable.h"
+#include "nsIFile.h"
 #include "nsIObserver.h"
 #include "nsString.h"
-#include "nsIFile.h"
+#include "nsTHashtable.h"
+#include "nsWeakReference.h"
 #include "secoidt.h"
-#include "nsWeakReference.h"
-#include "mozilla/Attributes.h"
 
 class nsCertOverride
 {
 public:
-
-  enum OverrideBits { ob_None=0, ob_Untrusted=1, ob_Mismatch=2,
-                      ob_Time_error=4 };
+  enum class OverrideBits {
+    None = 0,
+    Untrusted = nsICertOverrideService::ERROR_UNTRUSTED,
+    Mismatch = nsICertOverrideService::ERROR_MISMATCH,
+    Time = nsICertOverrideService::ERROR_TIME,
+  };
 
   nsCertOverride()
-  :mPort(-1)
-  ,mOverrideBits(ob_None)
+    : mPort(-1)
+    , mOverrideBits(OverrideBits::None)
   {
   }
 
   nsCertOverride(const nsCertOverride &other)
   {
     this->operator=(other);
   }
 
@@ -56,16 +59,17 @@ public:
   OverrideBits mOverrideBits;
   nsCString mDBKey;
   nsCOMPtr <nsIX509Cert> mCert;
 
   static void convertBitsToString(OverrideBits ob, nsACString &str);
   static void convertStringToBits(const nsACString &str, OverrideBits &ob);
 };
 
+MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(nsCertOverride::OverrideBits)
 
 // hash entry class
 class nsCertOverrideEntry final : public PLDHashEntryHdr
 {
   public:
     // Hash methods
     typedef const char* KeyType;
     typedef const char* KeyTypePointer;
@@ -179,9 +183,9 @@ protected:
 
 #define NS_CERTOVERRIDE_CID { /* 67ba681d-5485-4fff-952c-2ee337ffdcd6 */ \
     0x67ba681d,                                                        \
     0x5485,                                                            \
     0x4fff,                                                            \
     {0x95, 0x2c, 0x2e, 0xe3, 0x37, 0xff, 0xdc, 0xd6}                   \
   }
 
-#endif
+#endif // nsCertOverrideService_h
--- a/security/manager/ssl/nsCertTree.cpp
+++ b/security/manager/ssl/nsCertTree.cpp
@@ -90,21 +90,21 @@ static const PLDHashTableOps gMapOps = {
   CompareCacheClearEntry,
   CompareCacheInitEntry
 };
 
 NS_IMPL_ISUPPORTS0(nsCertAddonInfo)
 NS_IMPL_ISUPPORTS(nsCertTreeDispInfo, nsICertTreeItem)
 
 nsCertTreeDispInfo::nsCertTreeDispInfo()
-:mAddonInfo(nullptr)
-,mTypeOfEntry(direct_db)
-,mPort(-1)
-,mOverrideBits(nsCertOverride::ob_None)
-,mIsTemporary(true)
+  : mAddonInfo(nullptr)
+  , mTypeOfEntry(direct_db)
+  , mPort(-1)
+  , mOverrideBits(nsCertOverride::OverrideBits::None)
+  , mIsTemporary(true)
 {
 }
 
 nsCertTreeDispInfo::nsCertTreeDispInfo(nsCertTreeDispInfo &other)
 {
   mAddonInfo = other.mAddonInfo;
   mTypeOfEntry = other.mTypeOfEntry;
   mAsciiHost = other.mAsciiHost;
@@ -567,17 +567,17 @@ nsCertTree::GetCertsByTypeFromCertList(C
         }
       }
       if (wantThisCert) {
         nsCertTreeDispInfo *certdi = new nsCertTreeDispInfo;
         certdi->mAddonInfo = certai;
         certai->mUsageCount++;
         certdi->mTypeOfEntry = nsCertTreeDispInfo::direct_db;
         // not necessary: certdi->mAsciiHost.Clear(); certdi->mPort = -1;
-        certdi->mOverrideBits = nsCertOverride::ob_None;
+        certdi->mOverrideBits = nsCertOverride::OverrideBits::None;
         certdi->mIsTemporary = false;
         mDispInfo.InsertElementAt(InsertPosition, certdi);
         ++count;
         ++InsertPosition;
       }
       if (addOverrides) {
         nsCertAndArrayAndPositionAndCounterAndTracker cap;
         cap.certai = certai;