Bug 1253010 - part 1 - refactor nsX509CertValidity time formatting; r=keeler
authorNathan Froyd <froydnj@mozilla.com>
Sat, 05 Dec 2015 10:26:19 -0500
changeset 287277 d3bc99831ce627675c2efbb168ebd5b82b4ff7ee
parent 287276 96018aec0a709ed554bd0f6a3d4e6bba28ed0b8b
child 287278 22d7c93ec6579884fa9468e5f5503387cb23cc0a
push id73140
push usernfroyd@mozilla.com
push dateTue, 08 Mar 2016 21:00:12 +0000
treeherdermozilla-inbound@051996f60967 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskeeler
bugs1253010
milestone47.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1253010 - part 1 - refactor nsX509CertValidity time formatting; r=keeler nsX509CertValidity has several copy-pasted routines that differ only slightly in the parameters they use for formatting times. Let's have a single place to do the formatting and pass in the appropriate parameters.
security/manager/ssl/nsNSSCertValidity.cpp
security/manager/ssl/nsNSSCertValidity.h
--- a/security/manager/ssl/nsNSSCertValidity.cpp
+++ b/security/manager/ssl/nsNSSCertValidity.cpp
@@ -42,134 +42,77 @@ NS_IMETHODIMP nsX509CertValidity::GetNot
   nsresult rv = NS_ERROR_FAILURE;
   if (mTimesInitialized) {
     *aNotBefore = mNotBefore;
     rv = NS_OK;
   }
   return rv;
 }
 
-NS_IMETHODIMP nsX509CertValidity::GetNotBeforeLocalTime(nsAString &aNotBeforeLocalTime)
+nsresult
+nsX509CertValidity::FormatTime(const PRTime& aTimeDate,
+                               PRTimeParamFn aParamFn,
+                               const nsTimeFormatSelector aTimeFormatSelector,
+                               nsAString& aFormattedTimeDate)
 {
   if (!mTimesInitialized)
     return NS_ERROR_FAILURE;
 
   nsresult rv;
   nsCOMPtr<nsIDateTimeFormat> dateFormatter =
      do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv);
   if (NS_FAILED(rv)) return rv;
 
-  nsAutoString date;
   PRExplodedTime explodedTime;
-  PR_ExplodeTime(mNotBefore, PR_LocalTimeParameters, &explodedTime);
-  dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatLong,
-                                      kTimeFormatSeconds, &explodedTime, date);
-  aNotBeforeLocalTime = date;
-  return NS_OK;
+  PR_ExplodeTime(const_cast<PRTime&>(aTimeDate), aParamFn, &explodedTime);
+  return dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatLong,
+					     aTimeFormatSelector,
+					     &explodedTime, aFormattedTimeDate);
+}
+
+NS_IMETHODIMP nsX509CertValidity::GetNotBeforeLocalTime(nsAString &aNotBeforeLocalTime)
+{
+  return FormatTime(mNotBefore, PR_LocalTimeParameters,
+                    kTimeFormatSeconds, aNotBeforeLocalTime);
 }
 
 NS_IMETHODIMP nsX509CertValidity::GetNotBeforeLocalDay(nsAString &aNotBeforeLocalDay)
 {
-  if (!mTimesInitialized)
-    return NS_ERROR_FAILURE;
-
-  nsresult rv;
-  nsCOMPtr<nsIDateTimeFormat> dateFormatter =
-     do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv);
-  if (NS_FAILED(rv)) return rv;
-
-  nsAutoString date;
-  PRExplodedTime explodedTime;
-  PR_ExplodeTime(mNotBefore, PR_LocalTimeParameters, &explodedTime);
-  dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatLong, kTimeFormatNone,
-                                      &explodedTime, date);
-  aNotBeforeLocalDay = date;
-  return NS_OK;
+  return FormatTime(mNotBefore, PR_LocalTimeParameters,
+                    kTimeFormatNone, aNotBeforeLocalDay);
 }
 
 
 NS_IMETHODIMP nsX509CertValidity::GetNotBeforeGMT(nsAString &aNotBeforeGMT)
 {
-  if (!mTimesInitialized)
-    return NS_ERROR_FAILURE;
-
-  nsresult rv;
-  nsCOMPtr<nsIDateTimeFormat> dateFormatter =
-     do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv);
-  if (NS_FAILED(rv)) return rv;
-
-  nsAutoString date;
-  PRExplodedTime explodedTime;
-  PR_ExplodeTime(mNotBefore, PR_GMTParameters, &explodedTime);
-  dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatLong,
-                                      kTimeFormatSeconds, &explodedTime, date);
-  aNotBeforeGMT = date;
-  return NS_OK;
+  return FormatTime(mNotBefore, PR_GMTParameters,
+                    kTimeFormatSeconds, aNotBeforeGMT);
 }
 
 NS_IMETHODIMP nsX509CertValidity::GetNotAfter(PRTime *aNotAfter)
 {
   NS_ENSURE_ARG(aNotAfter);
 
   nsresult rv = NS_ERROR_FAILURE;
   if (mTimesInitialized) {
     *aNotAfter = mNotAfter;
     rv = NS_OK;
   }
   return rv;
 }
 
 NS_IMETHODIMP nsX509CertValidity::GetNotAfterLocalTime(nsAString &aNotAfterLocaltime)
 {
-  if (!mTimesInitialized)
-    return NS_ERROR_FAILURE;
-
-  nsresult rv;
-  nsCOMPtr<nsIDateTimeFormat> dateFormatter =
-     do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv);
-  if (NS_FAILED(rv)) return rv;
-
-  nsAutoString date;
-  PRExplodedTime explodedTime;
-  PR_ExplodeTime(mNotAfter, PR_LocalTimeParameters, &explodedTime);
-  dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatLong,
-                                      kTimeFormatSeconds, &explodedTime, date);
-  aNotAfterLocaltime = date;
-  return NS_OK;
+  return FormatTime(mNotAfter, PR_LocalTimeParameters,
+                    kTimeFormatSeconds, aNotAfterLocaltime);
 }
 
 NS_IMETHODIMP nsX509CertValidity::GetNotAfterLocalDay(nsAString &aNotAfterLocalDay)
 {
-  if (!mTimesInitialized)
-    return NS_ERROR_FAILURE;
-
-  nsresult rv;
-  nsCOMPtr<nsIDateTimeFormat> dateFormatter =
-     do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv);
-  if (NS_FAILED(rv)) return rv;
-
-  nsAutoString date;
-  PRExplodedTime explodedTime;
-  PR_ExplodeTime(mNotAfter, PR_LocalTimeParameters, &explodedTime);
-  dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatLong, kTimeFormatNone,
-                                      &explodedTime, date);
-  aNotAfterLocalDay = date;
-  return NS_OK;
+  return FormatTime(mNotAfter, PR_LocalTimeParameters,
+                    kTimeFormatNone, aNotAfterLocalDay);
 }
 
 NS_IMETHODIMP nsX509CertValidity::GetNotAfterGMT(nsAString &aNotAfterGMT)
 {
-  if (!mTimesInitialized)
-    return NS_ERROR_FAILURE;
-
-  nsresult rv;
-  nsCOMPtr<nsIDateTimeFormat> dateFormatter =
-     do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID, &rv);
-  if (NS_FAILED(rv)) return rv;
-
-  nsAutoString date;
-  PRExplodedTime explodedTime;
-  PR_ExplodeTime(mNotAfter, PR_GMTParameters, &explodedTime);
-  dateFormatter->FormatPRExplodedTime(nullptr, kDateFormatLong,
-                                      kTimeFormatSeconds, &explodedTime, date);
-  aNotAfterGMT = date;
-  return NS_OK;
+  return FormatTime(mNotAfter, PR_GMTParameters,
+                    kTimeFormatSeconds, aNotAfterGMT);
 }
--- a/security/manager/ssl/nsNSSCertValidity.h
+++ b/security/manager/ssl/nsNSSCertValidity.h
@@ -1,15 +1,16 @@
 /* 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 _NSX509CERTVALIDITY_H_
 #define _NSX509CERTVALIDITY_H_
 
+#include "nsIDateTimeFormat.h"
 #include "nsIX509CertValidity.h"
 
 #include "certt.h"
 
 class nsX509CertValidity : public nsIX509CertValidity
 {
 public:
   NS_DECL_THREADSAFE_ISUPPORTS
@@ -18,13 +19,18 @@ public:
   nsX509CertValidity();
   explicit nsX509CertValidity(CERTCertificate *cert);
 
 protected:
   virtual ~nsX509CertValidity();
   /* additional members */
 
 private:
+  nsresult FormatTime(const PRTime& aTime,
+                      PRTimeParamFn aParamFn,
+                      const nsTimeFormatSelector aTimeFormatSelector,
+                      nsAString& aFormattedTimeDate);
+
   PRTime mNotBefore, mNotAfter;
   bool mTimesInitialized;
 };
 
 #endif