Bug 1178991 - smartptr for http converter r=hurley
authorPatrick McManus <mcmanus@ducksong.com>
Tue, 30 Jun 2015 17:17:10 -0400
changeset 285271 418460bc829baf3eb4ea8ed5818ed0b178590335
parent 285270 d9ea769d96b8910c9dd3430cfe6b0db7e6c7a094
child 285272 7534fa58817f452550792563c6aee258649c9982
push id934
push userraliiev@mozilla.com
push dateMon, 26 Oct 2015 12:58:05 +0000
treeherdermozilla-release@05704e35c1d0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewershurley
bugs1178991
milestone42.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 1178991 - smartptr for http converter r=hurley
netwerk/streamconv/converters/nsHTTPCompressConv.cpp
netwerk/streamconv/converters/nsHTTPCompressConv.h
--- a/netwerk/streamconv/converters/nsHTTPCompressConv.cpp
+++ b/netwerk/streamconv/converters/nsHTTPCompressConv.cpp
@@ -10,27 +10,25 @@
 #include "nsCOMPtr.h"
 #include "nsError.h"
 #include "nsStreamUtils.h"
 #include "nsStringStream.h"
 #include "nsComponentManagerUtils.h"
 #include "nsThreadUtils.h"
 #include "mozilla/Preferences.h"
 
-
 // nsISupports implementation
 NS_IMPL_ISUPPORTS(nsHTTPCompressConv,
                   nsIStreamConverter,
                   nsIStreamListener,
                   nsIRequestObserver)
 
 // nsFTPDirListingConv methods
 nsHTTPCompressConv::nsHTTPCompressConv()
-    : mListener(nullptr)
-    , mMode(HTTP_COMPRESS_IDENTITY)
+    : mMode(HTTP_COMPRESS_IDENTITY)
     , mOutBuffer(nullptr)
     , mInpBuffer(nullptr)
     , mOutBufferLen(0)
     , mInpBufferLen(0)
     , mCheckHeaderDone(false)
     , mStreamEnded(false)
     , mStreamInitialized(false)
     , mLen(0)
@@ -43,81 +41,77 @@ nsHTTPCompressConv::nsHTTPCompressConv()
             Preferences::GetBool("network.http.enforce-framing.http", false);
     } else {
         mFailUncleanStops = false;
     }
 }
 
 nsHTTPCompressConv::~nsHTTPCompressConv()
 {
-    NS_IF_RELEASE(mListener);
-
     if (mInpBuffer)
         free(mInpBuffer);
 
     if (mOutBuffer)
         free(mOutBuffer);
 
     // For some reason we are not getting Z_STREAM_END.  But this was also seen
     //    for mozilla bug 198133.  Need to handle this case.
     if (mStreamInitialized && !mStreamEnded)
         inflateEnd (&d_stream);
 }
 
 NS_IMETHODIMP
-nsHTTPCompressConv::AsyncConvertData(const char *aFromType, 
-                                     const char *aToType, 
-                                     nsIStreamListener *aListener, 
+nsHTTPCompressConv::AsyncConvertData(const char *aFromType,
+                                     const char *aToType,
+                                     nsIStreamListener *aListener,
                                      nsISupports *aCtxt)
 {
     if (!PL_strncasecmp(aFromType, HTTP_COMPRESS_TYPE, sizeof(HTTP_COMPRESS_TYPE)-1) ||
-        !PL_strncasecmp(aFromType, HTTP_X_COMPRESS_TYPE, sizeof(HTTP_X_COMPRESS_TYPE)-1))
+        !PL_strncasecmp(aFromType, HTTP_X_COMPRESS_TYPE, sizeof(HTTP_X_COMPRESS_TYPE)-1)) {
         mMode = HTTP_COMPRESS_COMPRESS;
-
-    else if (!PL_strncasecmp(aFromType, HTTP_GZIP_TYPE, sizeof(HTTP_GZIP_TYPE)-1) ||
-             !PL_strncasecmp(aFromType, HTTP_X_GZIP_TYPE, sizeof(HTTP_X_GZIP_TYPE)-1))
+    } else if (!PL_strncasecmp(aFromType, HTTP_GZIP_TYPE, sizeof(HTTP_GZIP_TYPE)-1) ||
+              !PL_strncasecmp(aFromType, HTTP_X_GZIP_TYPE, sizeof(HTTP_X_GZIP_TYPE)-1)) {
         mMode = HTTP_COMPRESS_GZIP;
+    } else if (!PL_strncasecmp(aFromType, HTTP_DEFLATE_TYPE, sizeof(HTTP_DEFLATE_TYPE)-1)) {
+        mMode = HTTP_COMPRESS_DEFLATE;
+    }
 
-    else if (!PL_strncasecmp(aFromType, HTTP_DEFLATE_TYPE, sizeof(HTTP_DEFLATE_TYPE)-1))
-        mMode = HTTP_COMPRESS_DEFLATE;
-
-    // hook ourself up with the receiving listener. 
+    // hook ourself up with the receiving listener.
     mListener = aListener;
-    NS_ADDREF(mListener);
 
     mAsyncConvContext = aCtxt;
-    return NS_OK; 
-} 
+    return NS_OK;
+}
 
 NS_IMETHODIMP
 nsHTTPCompressConv::OnStartRequest(nsIRequest* request, nsISupports *aContext)
 {
     return mListener->OnStartRequest(request, aContext);
-} 
+}
 
 NS_IMETHODIMP
-nsHTTPCompressConv::OnStopRequest(nsIRequest* request, nsISupports *aContext, 
+nsHTTPCompressConv::OnStopRequest(nsIRequest* request, nsISupports *aContext,
                                   nsresult aStatus)
 {
     // Framing integrity is enforced for content-encoding: gzip, but not for
     // content-encoding: deflate. Note that gzip vs deflate is NOT determined
     // by content sniffing but only via header.
     if (!mStreamEnded && NS_SUCCEEDED(aStatus) &&
         (mFailUncleanStops && (mMode == HTTP_COMPRESS_GZIP)) ) {
         // This is not a clean end of gzip stream: the transfer is incomplete.
         aStatus = NS_ERROR_NET_PARTIAL_TRANSFER;
     }
     return mListener->OnStopRequest(request, aContext, aStatus);
-} 
+}
 
 NS_IMETHODIMP
-nsHTTPCompressConv::OnDataAvailable(nsIRequest* request, 
-                                    nsISupports *aContext, 
-                                    nsIInputStream *iStr, 
-                                    uint64_t aSourceOffset, 
+nsHTTPCompressConv::OnDataAvailable(nsIRequest* request,
+                                    nsISupports *aContext,
+                                    nsIInputStream *iStr,
+                                    uint64_t aSourceOffset,
                                     uint32_t aCount)
 {
     nsresult rv = NS_ERROR_INVALID_CONTENT_ENCODING;
     uint32_t streamLen = aCount;
 
     if (streamLen == 0)
     {
         NS_ERROR("count of zero passed to OnDataAvailable");
@@ -146,17 +140,17 @@ nsHTTPCompressConv::OnDataAvailable(nsIR
 
             // FALLTHROUGH
 
         case HTTP_COMPRESS_DEFLATE:
 
             if (mInpBuffer != nullptr && streamLen > mInpBufferLen)
             {
                 mInpBuffer = (unsigned char *) realloc(mInpBuffer, mInpBufferLen = streamLen);
-               
+
                 if (mOutBufferLen < streamLen * 2)
                     mOutBuffer = (unsigned char *) realloc(mOutBuffer, mOutBufferLen = streamLen * 3);
 
                 if (mInpBuffer == nullptr || mOutBuffer == nullptr)
                     return NS_ERROR_OUT_OF_MEMORY;
             }
 
             if (mInpBuffer == nullptr)
@@ -171,43 +165,43 @@ nsHTTPCompressConv::OnDataAvailable(nsIR
             uint32_t unused;
             iStr->Read((char *)mInpBuffer, streamLen, &unused);
 
             if (mMode == HTTP_COMPRESS_DEFLATE)
             {
                 if (!mStreamInitialized)
                 {
                     memset(&d_stream, 0, sizeof (d_stream));
-                
+
                     if (inflateInit(&d_stream) != Z_OK)
                         return NS_ERROR_FAILURE;
 
                     mStreamInitialized = true;
                 }
                 d_stream.next_in = mInpBuffer;
                 d_stream.avail_in = (uInt)streamLen;
 
                 mDummyStreamInitialised = false;
                 for (;;)
                 {
                     d_stream.next_out = mOutBuffer;
                     d_stream.avail_out = (uInt)mOutBufferLen;
- 
+
                     int code = inflate(&d_stream, Z_NO_FLUSH);
                     unsigned bytesWritten = (uInt)mOutBufferLen - d_stream.avail_out;
 
                     if (code == Z_STREAM_END)
                     {
                         if (bytesWritten)
                         {
                             rv = do_OnDataAvailable(request, aContext, aSourceOffset, (char *)mOutBuffer, bytesWritten);
                             if (NS_FAILED (rv))
                                 return rv;
                         }
-                        
+
                         inflateEnd(&d_stream);
                         mStreamEnded = true;
                         break;
                     }
                     else if (code == Z_OK)
                     {
                         if (bytesWritten)
                         {
@@ -248,53 +242,53 @@ nsHTTPCompressConv::OnDataAvailable(nsIR
                             NS_WARNING("endless loop detected"
                                        " - invalid deflate");
                             return NS_ERROR_INVALID_CONTENT_ENCODING;
                         }
                         mDummyStreamInitialised = true;
                         // reset stream pointers to our original data
                         d_stream.next_in = mInpBuffer;
                         d_stream.avail_in = (uInt)streamLen;
-                    }    
-                    else
+                    } else {
                         return NS_ERROR_INVALID_CONTENT_ENCODING;
+                    }
                 } /* for */
             }
             else
             {
                 if (!mStreamInitialized)
                 {
                     memset(&d_stream, 0, sizeof (d_stream));
-                
+
                     if (inflateInit2(&d_stream, -MAX_WBITS) != Z_OK)
                         return NS_ERROR_FAILURE;
 
                     mStreamInitialized = true;
                 }
 
                 d_stream.next_in  = mInpBuffer;
                 d_stream.avail_in = (uInt)streamLen;
 
                 for (;;)
                 {
                     d_stream.next_out  = mOutBuffer;
                     d_stream.avail_out = (uInt)mOutBufferLen;
- 
+
                     int code = inflate (&d_stream, Z_NO_FLUSH);
                     unsigned bytesWritten = (uInt)mOutBufferLen - d_stream.avail_out;
 
                     if (code == Z_STREAM_END)
                     {
                         if (bytesWritten)
                         {
                             rv = do_OnDataAvailable(request, aContext, aSourceOffset, (char *)mOutBuffer, bytesWritten);
                             if (NS_FAILED (rv))
                                 return rv;
                         }
-                        
+
                         inflateEnd(&d_stream);
                         mStreamEnded = true;
                         break;
                     }
                     else if (code == Z_OK)
                     {
                         if (bytesWritten)
                         {
@@ -314,37 +308,37 @@ nsHTTPCompressConv::OnDataAvailable(nsIR
                         break;
                     }
                     else
                         return NS_ERROR_INVALID_CONTENT_ENCODING;
                 } /* for */
             } /* gzip */
             break;
 
-        default: 
+        default:
             rv = mListener->OnDataAvailable(request, aContext, iStr, aSourceOffset, aCount);
             if (NS_FAILED (rv))
                 return rv;
     } /* switch */
 
 	return NS_OK;
 } /* OnDataAvailable */
 
 
 // XXX/ruslan: need to implement this too
 
 NS_IMETHODIMP
-nsHTTPCompressConv::Convert(nsIInputStream *aFromStream, 
-                            const char *aFromType, 
-                            const char *aToType, 
-                            nsISupports *aCtxt, 
+nsHTTPCompressConv::Convert(nsIInputStream *aFromStream,
+                            const char *aFromType,
+                            const char *aToType,
+                            nsISupports *aCtxt,
                             nsIInputStream **_retval)
-{ 
+{
     return NS_ERROR_NOT_IMPLEMENTED;
-} 
+}
 
 nsresult
 nsHTTPCompressConv::do_OnDataAvailable(nsIRequest* request,
                                        nsISupports *context, uint64_t offset,
                                        const char *buffer, uint32_t count)
 {
     if (!mStream) {
         mStream = do_CreateInstance(NS_STRINGINPUTSTREAM_CONTRACTID);
@@ -386,17 +380,17 @@ nsHTTPCompressConv::check_header(nsIInpu
     while (streamLen)
     {
         switch (hMode)
         {
             case GZIP_INIT:
                 uint32_t unused;
                 iStr->Read(&c, 1, &unused);
                 streamLen--;
-                
+
                 if (mSkipCount == 0 && ((unsigned)c & 0377) != gz_magic[0])
                 {
                     *rs = NS_ERROR_INVALID_CONTENT_ENCODING;
                     return 0;
                 }
 
                 if (mSkipCount == 1 && ((unsigned)c & 0377) != gz_magic[1])
                 {
@@ -427,17 +421,17 @@ nsHTTPCompressConv::check_header(nsIInpu
             case GZIP_OS:
                 iStr->Read(&c, 1, &unused);
                 streamLen--;
                 mSkipCount++;
 
                 if (mSkipCount == 6)
                     hMode = GZIP_EXTRA0;
                 break;
-        
+
             case GZIP_EXTRA0:
                 if (mFlags & EXTRA_FIELD)
                 {
                     iStr->Read(&c, 1, &unused);
                     streamLen--;
                     mLen = (uInt) c & 0377;
                     hMode = GZIP_EXTRA1;
                 }
--- a/netwerk/streamconv/converters/nsHTTPCompressConv.h
+++ b/netwerk/streamconv/converters/nsHTTPCompressConv.h
@@ -38,38 +38,37 @@ typedef enum    {
         HTTP_COMPRESS_IDENTITY
     }   CompressMode;
 
 class nsHTTPCompressConv	: public nsIStreamConverter	{
 public:
     // nsISupports methods
     NS_DECL_THREADSAFE_ISUPPORTS
 
-	NS_DECL_NSIREQUESTOBSERVER
+    NS_DECL_NSIREQUESTOBSERVER
     NS_DECL_NSISTREAMLISTENER
 
     // nsIStreamConverter methods
     NS_DECL_NSISTREAMCONVERTER
 
-
     nsHTTPCompressConv ();
 
 private:
 
     virtual ~nsHTTPCompressConv ();
 
-    nsIStreamListener   *mListener; // this guy gets the converted data via his OnDataAvailable ()
-	CompressMode        mMode;
+    nsCOMPtr<nsIStreamListener> mListener; // this guy gets the converted data via his OnDataAvailable ()
+    CompressMode        mMode;
 
     unsigned char *mOutBuffer;
     unsigned char *mInpBuffer;
 
     uint32_t	mOutBufferLen;
     uint32_t	mInpBufferLen;
-	
+
     nsCOMPtr<nsISupports>   mAsyncConvContext;
     nsCOMPtr<nsIStringInputStream>  mStream;
 
     nsresult do_OnDataAvailable (nsIRequest *request, nsISupports *aContext,
                                  uint64_t aSourceOffset, const char *buffer,
                                  uint32_t aCount);
 
     bool        mCheckHeaderDone;