Bug 635977. Make sure to always send OnStartRequest to our listener from nsIndexedToHTML. r=jduell, a=dveditz
authorBoris Zbarsky <bzbarsky@mit.edu>
Thu, 24 Feb 2011 13:42:15 -0500
changeset 27341 4447edf333a996dacc3630b751c97d1de8ed6d0f
parent 27340 d9a42eb3a7a58ee9a2a4cb9b7a08549c349f6197
child 27342 247626dd30db41d24474ac26369ff2829554fa13
push id2676
push userbzbarsky@mozilla.com
push dateFri, 25 Feb 2011 18:43:24 +0000
reviewersjduell, dveditz
bugs635977
milestone1.9.1.18pre
Bug 635977. Make sure to always send OnStartRequest to our listener from nsIndexedToHTML. r=jduell, a=dveditz
netwerk/streamconv/converters/nsIndexedToHTML.cpp
netwerk/streamconv/converters/nsIndexedToHTML.h
--- a/netwerk/streamconv/converters/nsIndexedToHTML.cpp
+++ b/netwerk/streamconv/converters/nsIndexedToHTML.cpp
@@ -145,16 +145,39 @@ nsIndexedToHTML::AsyncConvertData(const 
                                   const char *aToType,
                                   nsIStreamListener *aListener,
                                   nsISupports *aCtxt) {
     return Init(aListener);
 }
 
 NS_IMETHODIMP
 nsIndexedToHTML::OnStartRequest(nsIRequest* request, nsISupports *aContext) {
+    nsString buffer;
+    nsresult rv = DoOnStartRequest(request, aContext, buffer);
+    if (NS_FAILED(rv)) {
+        request->Cancel(rv);
+    }
+    
+    rv = mListener->OnStartRequest(request, aContext);
+    if (NS_FAILED(rv)) return rv;
+
+    // The request may have been canceled, and if that happens, we want to
+    // suppress calls to OnDataAvailable.
+    request->GetStatus(&rv);
+    if (NS_FAILED(rv)) return rv;
+
+    // Push our buffer to the listener.
+
+    rv = FormatInputStream(request, aContext, buffer);
+    return rv;
+}
+
+nsresult
+nsIndexedToHTML::DoOnStartRequest(nsIRequest* request, nsISupports *aContext,
+                                  nsString& aBuffer) {
     nsresult rv;
 
     nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
     nsCOMPtr<nsIURI> uri;
     rv = channel->GetURI(getter_AddRefs(uri));
     if (NS_FAILED(rv)) return rv;
 
     channel->SetContentType(NS_LITERAL_CSTRING("application/xhtml+xml"));
@@ -682,28 +705,17 @@ nsIndexedToHTML::OnStartRequest(nsIReque
         if (NS_FAILED(rv)) return rv;
         AppendNonAsciiToNCR(columnText, buffer);
         buffer.AppendLiteral("</th>\n"
                              "  </tr>\n"
                              " </thead>\n");
     }
     buffer.AppendLiteral(" <tbody>\n");
 
-    // Push buffer to the listener now, so the initial HTML will not
-    // be parsed in OnDataAvailable().
-
-    rv = mListener->OnStartRequest(request, aContext);
-    if (NS_FAILED(rv)) return rv;
-
-    // The request may have been canceled, and if that happens, we want to
-    // suppress calls to OnDataAvailable.
-    request->GetStatus(&rv);
-    if (NS_FAILED(rv)) return rv;
-
-    rv = FormatInputStream(request, aContext, buffer);
+    aBuffer = buffer;
     return rv;
 }
 
 NS_IMETHODIMP
 nsIndexedToHTML::OnStopRequest(nsIRequest* request, nsISupports *aContext,
                                nsresult aStatus) {
     if (NS_SUCCEEDED(aStatus)) {
         nsString buffer;
--- a/netwerk/streamconv/converters/nsIndexedToHTML.h
+++ b/netwerk/streamconv/converters/nsIndexedToHTML.h
@@ -71,17 +71,20 @@ public:
     nsresult Init(nsIStreamListener *aListener);
 
     static NS_METHOD
     Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);
 
 protected:
     
     void FormatSizeString(PRInt64 inSize, nsString& outSizeString);
-    nsresult FormatInputStream(nsIRequest* aRequest, nsISupports *aContext, const nsAString &aBuffer); 
+    nsresult FormatInputStream(nsIRequest* aRequest, nsISupports *aContext, const nsAString &aBuffer);
+    // Helper to properly implement OnStartRequest
+    nsresult DoOnStartRequest(nsIRequest* request, nsISupports *aContext,
+                              nsString& aBuffer);
 
 protected:
     nsCOMPtr<nsIDirIndexParser>     mParser;
     nsCOMPtr<nsIStreamListener>     mListener; // final listener (consumer)
 
     nsCOMPtr<nsIDateTimeFormat> mDateTime;
     nsCOMPtr<nsIStringBundle> mBundle;