Bug 1732931 - Make nsDirIndexParser::gTextToSubURI a StaticRefPtr r=necko-reviewers,dragana
authorValentin Gosu <valentin.gosu@gmail.com>
Thu, 30 Sep 2021 13:49:59 +0000
changeset 593838 24caabdf61d3f43030a80abd31eb16b1ed1dd656
parent 593837 c18ceac2d1dc9da1777c6151c933a4fc8455b2a5
child 593839 8aea9a40a4667d072c7da6aeffae36833a9299b1
push id150642
push uservalentin.gosu@gmail.com
push dateThu, 30 Sep 2021 14:04:30 +0000
treeherderautoland@24caabdf61d3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnecko-reviewers, dragana
bugs1732931
milestone94.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 1732931 - Make nsDirIndexParser::gTextToSubURI a StaticRefPtr r=necko-reviewers,dragana Differential Revision: https://phabricator.services.mozilla.com/D126810
netwerk/streamconv/converters/nsDirIndexParser.cpp
netwerk/streamconv/converters/nsDirIndexParser.h
--- a/netwerk/streamconv/converters/nsDirIndexParser.cpp
+++ b/netwerk/streamconv/converters/nsDirIndexParser.cpp
@@ -3,17 +3,19 @@
  * 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/. */
 
 /* This parsing code originally lived in xpfe/components/directory/ - bbaetz */
 
 #include "nsDirIndexParser.h"
 
 #include "mozilla/ArrayUtils.h"
+#include "mozilla/ClearOnShutdown.h"
 #include "mozilla/Encoding.h"
+#include "mozilla/StaticPtr.h"
 #include "prprf.h"
 #include "nsCRT.h"
 #include "nsDirIndex.h"
 #include "nsEscape.h"
 #include "nsIDirIndex.h"
 #include "nsIInputStream.h"
 #include "nsITextToSubURI.h"
 #include "nsServiceManagerUtils.h"
@@ -21,16 +23,18 @@
 
 using namespace mozilla;
 
 struct EncodingProp {
   const char* const mKey;
   NotNull<const Encoding*> mValue;
 };
 
+static StaticRefPtr<nsITextToSubURI> gTextToSubURI;
+
 static const EncodingProp localesFallbacks[] = {
     {"ar", WINDOWS_1256_ENCODING}, {"ba", WINDOWS_1251_ENCODING},
     {"be", WINDOWS_1251_ENCODING}, {"bg", WINDOWS_1251_ENCODING},
     {"cs", WINDOWS_1250_ENCODING}, {"el", ISO_8859_7_ENCODING},
     {"et", WINDOWS_1257_ENCODING}, {"fa", WINDOWS_1256_ENCODING},
     {"he", WINDOWS_1255_ENCODING}, {"hr", WINDOWS_1250_ENCODING},
     {"hu", ISO_8859_2_ENCODING},   {"ja", SHIFT_JIS_ENCODING},
     {"kk", WINDOWS_1251_ENCODING}, {"ko", EUC_KR_ENCODING},
@@ -85,34 +89,29 @@ NS_IMPL_ISUPPORTS(nsDirIndexParser, nsIR
 
 nsresult nsDirIndexParser::Init() {
   mLineStart = 0;
   mHasDescription = false;
   mFormat[0] = -1;
   auto encoding = GetFTPFallbackEncodingDoNotAddNewCallersToThisFunction();
   encoding->Name(mEncoding);
 
-  nsresult rv;
-  // XXX not threadsafe
-  if (gRefCntParser++ == 0) {
-    rv = CallGetService(NS_ITEXTTOSUBURI_CONTRACTID, &gTextToSubURI);
-  } else {
-    rv = NS_OK;
+  nsresult rv = NS_OK;
+  if (!gTextToSubURI) {
+    nsCOMPtr<nsITextToSubURI> service =
+        do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv);
+    if (NS_SUCCEEDED(rv)) {
+      gTextToSubURI = service;
+      ClearOnShutdown(&gTextToSubURI);
+    }
   }
 
   return rv;
 }
 
-nsDirIndexParser::~nsDirIndexParser() {
-  // XXX not threadsafe
-  if (--gRefCntParser == 0) {
-    NS_IF_RELEASE(gTextToSubURI);
-  }
-}
-
 NS_IMETHODIMP
 nsDirIndexParser::SetListener(nsIDirIndexListener* aListener) {
   mListener = aListener;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsDirIndexParser::GetListener(nsIDirIndexListener** aListener) {
@@ -161,19 +160,16 @@ nsDirIndexParser::Field nsDirIndexParser
     {"Filename", FIELD_FILENAME},
     {"Description", FIELD_DESCRIPTION},
     {"Content-Length", FIELD_CONTENTLENGTH},
     {"Last-Modified", FIELD_LASTMODIFIED},
     {"Content-Type", FIELD_CONTENTTYPE},
     {"File-Type", FIELD_FILETYPE},
     {nullptr, FIELD_UNKNOWN}};
 
-nsrefcnt nsDirIndexParser::gRefCntParser = 0;
-nsITextToSubURI* nsDirIndexParser::gTextToSubURI = nullptr;
-
 void nsDirIndexParser::ParseFormat(const char* aFormatStr) {
   // Parse a "200" format line, and remember the fields and their
   // ordering in mFormat. Multiple 200 lines stomp on each other.
   unsigned int formatNum = 0;
   mFormat[0] = -1;
 
   do {
     while (*aFormatStr && nsCRT::IsAsciiSpace(char16_t(*aFormatStr))) {
@@ -276,20 +272,20 @@ void nsDirIndexParser::ParseData(nsIDirI
       case FIELD_FILENAME: {
         // don't unescape at this point, so that UnEscapeAndConvert() can
         filename = value;
 
         bool success = false;
 
         nsAutoString entryuri;
 
-        if (gTextToSubURI) {
+        if (RefPtr<nsITextToSubURI> textToSub = gTextToSubURI) {
           nsAutoString result;
-          if (NS_SUCCEEDED(gTextToSubURI->UnEscapeAndConvert(
-                  mEncoding, filename, result))) {
+          if (NS_SUCCEEDED(
+                  textToSub->UnEscapeAndConvert(mEncoding, filename, result))) {
             if (!result.IsEmpty()) {
               aIdx->SetLocation(filename);
               if (!mHasDescription) aIdx->SetDescription(result);
               success = true;
             }
           } else {
             NS_WARNING("UnEscapeAndConvert error");
           }
--- a/netwerk/streamconv/converters/nsDirIndexParser.h
+++ b/netwerk/streamconv/converters/nsDirIndexParser.h
@@ -13,17 +13,17 @@
 
 class nsIDirIndex;
 class nsITextToSubURI;
 
 /* CID: {a0d6ad32-1dd1-11b2-aa55-a40187b54036} */
 
 class nsDirIndexParser : public nsIDirIndexParser {
  private:
-  virtual ~nsDirIndexParser();
+  virtual ~nsDirIndexParser() = default;
 
   nsDirIndexParser() = default;
   nsresult Init();
 
  public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSISTREAMLISTENER
   NS_DECL_NSIREQUESTOBSERVER
@@ -62,14 +62,11 @@ class nsDirIndexParser : public nsIDirIn
   void ParseData(nsIDirIndex* aIdx, char* aDataStr, int32_t lineLen);
 
   struct Field {
     const char* mName;
     fieldType mType;
   };
 
   static Field gFieldTable[];
-
-  static nsrefcnt gRefCntParser;
-  static nsITextToSubURI* gTextToSubURI;
 };
 
 #endif