Bug 813438 - Part 3: Rewrite the unknown decoder to use NS_SniffContent; r=bzbarsky
authorEhsan Akhgari <ehsan@mozilla.com>
Tue, 20 Nov 2012 19:37:40 -0500
changeset 114042 1002aa671831f4b0b8fda7fa596807e9828cd8f9
parent 114041 883c4223dd20cdcc64429e9244c960029cc71e11
child 114043 2e3dfbc9d7988d9dc551047301ed227b3d5b8693
push id18512
push usereakhgari@mozilla.com
push dateThu, 22 Nov 2012 21:39:06 +0000
treeherdermozilla-inbound@2e3dfbc9d798 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbzbarsky
bugs813438
milestone20.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 813438 - Part 3: Rewrite the unknown decoder to use NS_SniffContent; r=bzbarsky
netwerk/streamconv/converters/nsUnknownDecoder.cpp
netwerk/streamconv/converters/nsUnknownDecoder.h
--- a/netwerk/streamconv/converters/nsUnknownDecoder.cpp
+++ b/netwerk/streamconv/converters/nsUnknownDecoder.cpp
@@ -12,25 +12,25 @@
 #include "nsIOutputStream.h"
 #include "nsMimeTypes.h"
 #include "netCore.h"
 #include "nsXPIDLString.h"
 #include "nsIPrefService.h"
 #include "nsIPrefBranch.h"
 #include "nsICategoryManager.h"
 #include "nsISupportsPrimitives.h"
-#include "nsIContentSniffer.h"
 
 #include "nsCRT.h"
 
 #include "nsIMIMEService.h"
 
 #include "nsIViewSourceChannel.h"
 #include "nsIHttpChannel.h"
 #include "nsNetCID.h"
+#include "nsNetUtil.h"
 
 
 #define MAX_BUFFER_SIZE 512
 
 nsUnknownDecoder::nsUnknownDecoder()
   : mBuffer(nullptr)
   , mBufferLen(0)
   , mRequireHTMLsuffix(false)
@@ -329,19 +329,19 @@ void nsUnknownDecoder::DetermineContentT
       if ((this->*(sSnifferEntries[i].mContentTypeSniffer))(aRequest)) {
         NS_ASSERTION(!mContentType.IsEmpty(), 
                      "Content type should be known by now.");
         return;
       }        
     }
   }
 
-  if (TryContentSniffers(aRequest)) {
-    NS_ASSERTION(!mContentType.IsEmpty(), 
-                 "Content type should be known by now.");
+  NS_SniffContent(NS_DATA_SNIFFER_CATEGORY, aRequest,
+                  (const uint8_t*)mBuffer, mBufferLen, mContentType);
+  if (!mContentType.IsEmpty()) {
     return;
   }
 
   if (SniffForHTML(aRequest)) {
     NS_ASSERTION(!mContentType.IsEmpty(), 
                  "Content type should be known by now.");
     return;
   }
@@ -354,59 +354,16 @@ void nsUnknownDecoder::DetermineContentT
     return;
   }
   
   LastDitchSniff(aRequest);
   NS_ASSERTION(!mContentType.IsEmpty(), 
                "Content type should be known by now.");
 }
 
-bool nsUnknownDecoder::TryContentSniffers(nsIRequest* aRequest)
-{
-  // Enumerate content sniffers
-  nsCOMPtr<nsICategoryManager> catMan(do_GetService("@mozilla.org/categorymanager;1"));
-  if (!catMan) {
-    return false;
-  }
-
-  nsCOMPtr<nsISimpleEnumerator> sniffers;
-  catMan->EnumerateCategory("content-sniffing-services", getter_AddRefs(sniffers));
-  if (!sniffers) {
-    return false;
-  }
-
-  bool hasMore;
-  while (NS_SUCCEEDED(sniffers->HasMoreElements(&hasMore)) && hasMore) {
-    nsCOMPtr<nsISupports> elem;
-    sniffers->GetNext(getter_AddRefs(elem));
-    NS_ASSERTION(elem, "No element even though hasMore returned true!?");
-
-    nsCOMPtr<nsISupportsCString> sniffer_id(do_QueryInterface(elem));
-    NS_ASSERTION(sniffer_id, "element is no nsISupportsCString!?");
-    nsAutoCString contractid;
-    nsresult rv = sniffer_id->GetData(contractid);
-    if (NS_FAILED(rv)) {
-      continue;
-    }
-
-    nsCOMPtr<nsIContentSniffer> sniffer(do_GetService(contractid.get()));
-    if (!sniffer) {
-      continue;
-    }
-
-    rv = sniffer->GetMIMETypeFromContent(aRequest, (const uint8_t*)mBuffer,
-                                         mBufferLen, mContentType);
-    if (NS_SUCCEEDED(rv)) {
-      return true;
-    }
-  }
-
-  return false;
-}
-
 bool nsUnknownDecoder::SniffForHTML(nsIRequest* aRequest)
 {
   /*
    * To prevent a possible attack, we will not consider this to be
    * html content if it comes from the local file system and our prefs
    * are set right
    */
   if (!AllowSniffing(aRequest)) {
--- a/netwerk/streamconv/converters/nsUnknownDecoder.h
+++ b/netwerk/streamconv/converters/nsUnknownDecoder.h
@@ -55,17 +55,16 @@ protected:
   // dangerous types (eg HTML) is ok for this request.  We can disable
   // sniffing for local files if needed using this.  Just a security
   // precation thingy... who knows when we suddenly need to flip this
   // pref?
   bool AllowSniffing(nsIRequest* aRequest);
   
   // Various sniffer functions.  Returning true means that a type
   // was determined; false means no luck.
-  bool TryContentSniffers(nsIRequest* aRequest);
   bool SniffForHTML(nsIRequest* aRequest);
   bool SniffForXML(nsIRequest* aRequest);
 
   // SniffURI guesses at the content type based on the URI (typically
   // using the extentsion)
   bool SniffURI(nsIRequest* aRequest);
 
   // LastDitchSniff guesses at text/plain vs. application/octet-stream