Bug 1209658 part 1. Refactor some parts of nsContentDLF to reduce code duplication and improve readability a bit. r=bkelly
authorBoris Zbarsky <bzbarsky@mit.edu>
Mon, 05 Oct 2015 12:23:25 -0400
changeset 301319 5f583313a353232b542a82f957cd6cac905adefa
parent 301318 06b3c63c39aa74bce1b83316b19d9920405c959b
child 301320 96c02250f071d33aadaa493aad3cd32dcd1fb4c9
push id1001
push userraliiev@mozilla.com
push dateMon, 18 Jan 2016 19:06:03 +0000
treeherdermozilla-release@8b89261f3ac4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly
bugs1209658
milestone44.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 1209658 part 1. Refactor some parts of nsContentDLF to reduce code duplication and improve readability a bit. r=bkelly
dom/base/nsContentUtils.cpp
layout/build/nsContentDLF.cpp
--- a/dom/base/nsContentUtils.cpp
+++ b/dom/base/nsContentUtils.cpp
@@ -3597,16 +3597,18 @@ nsContentUtils::IsChildOfSameType(nsIDoc
     docShellAsItem->GetSameTypeParent(getter_AddRefs(sameTypeParent));
   }
   return sameTypeParent != nullptr;
 }
 
 bool
 nsContentUtils::IsPlainTextType(const nsACString& aContentType)
 {
+  // NOTE: if you add a type here, add it to the CONTENTDLF_CATEGORIES
+  // define in nsContentDLF.h as well.
   return aContentType.EqualsLiteral(TEXT_PLAIN) ||
          aContentType.EqualsLiteral(TEXT_CSS) ||
          aContentType.EqualsLiteral(TEXT_CACHE_MANIFEST) ||
          aContentType.EqualsLiteral(APPLICATION_JAVASCRIPT) ||
          aContentType.EqualsLiteral(APPLICATION_XJAVASCRIPT) ||
          aContentType.EqualsLiteral(TEXT_ECMASCRIPT) ||
          aContentType.EqualsLiteral(APPLICATION_ECMASCRIPT) ||
          aContentType.EqualsLiteral(TEXT_JAVASCRIPT) ||
--- a/layout/build/nsContentDLF.cpp
+++ b/layout/build/nsContentDLF.cpp
@@ -43,30 +43,18 @@ static NS_DEFINE_IID(kHTMLDocumentCID, N
 static NS_DEFINE_IID(kXMLDocumentCID, NS_XMLDOCUMENT_CID);
 static NS_DEFINE_IID(kSVGDocumentCID, NS_SVGDOCUMENT_CID);
 static NS_DEFINE_IID(kVideoDocumentCID, NS_VIDEODOCUMENT_CID);
 static NS_DEFINE_IID(kImageDocumentCID, NS_IMAGEDOCUMENT_CID);
 static NS_DEFINE_IID(kXULDocumentCID, NS_XULDOCUMENT_CID);
 
 already_AddRefed<nsIContentViewer> NS_NewContentViewer();
 
-// XXXbz if you change the MIME types here, be sure to update
-// nsIParser.h and DetermineParseMode in nsParser.cpp and
-// nsHTMLDocument::StartDocumentLoad accordingly.
 static const char* const gHTMLTypes[] = {
   TEXT_HTML,
-  TEXT_PLAIN,
-  TEXT_CACHE_MANIFEST,
-  TEXT_CSS,
-  TEXT_JAVASCRIPT,
-  TEXT_ECMASCRIPT,
-  APPLICATION_JAVASCRIPT,
-  APPLICATION_ECMASCRIPT,
-  APPLICATION_XJAVASCRIPT,
-  APPLICATION_JSON,
   VIEWSOURCE_CONTENT_TYPE,
   APPLICATION_XHTML_XML,
   0
 };
   
 static const char* const gXMLTypes[] = {
   TEXT_XML,
   APPLICATION_XML,
@@ -82,16 +70,29 @@ static const char* const gSVGTypes[] = {
 };
 
 static const char* const gXULTypes[] = {
   TEXT_XUL,
   APPLICATION_CACHED_XUL,
   0
 };
 
+static bool
+IsTypeInList(const nsACString& aType, const char* const aList[])
+{
+  int32_t typeIndex;
+  for (typeIndex = 0; aList[typeIndex]; ++typeIndex) {
+    if (aType.Equals(aList[typeIndex])) {
+      return true;
+    }
+  }
+
+  return false;
+}
+
 nsresult
 NS_NewContentDocumentLoaderFactory(nsIDocumentLoaderFactory** aResult)
 {
   NS_PRECONDITION(aResult, "null OUT ptr");
   if (!aResult) {
     return NS_ERROR_NULL_POINTER;
   }
   nsContentDLF* it = new nsContentDLF();
@@ -149,100 +150,71 @@ nsContentDLF::CreateInstance(const char*
     aCommand = "view-source";
 
     // The parser freaks out when it sees the content-type that a
     // view-source channel normally returns.  Get the actual content
     // type of the data.  If it's known, use it; otherwise use
     // text/plain.
     nsAutoCString type;
     viewSourceChannel->GetOriginalContentType(type);
-    bool knownType = false;
-    int32_t typeIndex;
-    for (typeIndex = 0; gHTMLTypes[typeIndex] && !knownType; ++typeIndex) {
-      if (type.Equals(gHTMLTypes[typeIndex]) &&
-          !type.EqualsLiteral(VIEWSOURCE_CONTENT_TYPE)) {
-        knownType = true;
-      }
-    }
-
-    for (typeIndex = 0; gXMLTypes[typeIndex] && !knownType; ++typeIndex) {
-      if (type.Equals(gXMLTypes[typeIndex])) {
-        knownType = true;
-      }
-    }
-
-    for (typeIndex = 0; gSVGTypes[typeIndex] && !knownType; ++typeIndex) {
-      if (type.Equals(gSVGTypes[typeIndex])) {
-        knownType = true;
-      }
-    }
-
-    for (typeIndex = 0; gXULTypes[typeIndex] && !knownType; ++typeIndex) {
-      if (type.Equals(gXULTypes[typeIndex])) {
-        knownType = true;
-      }
-    }
+    bool knownType =
+      (!type.EqualsLiteral(VIEWSOURCE_CONTENT_TYPE) &&
+        IsTypeInList(type, gHTMLTypes)) ||
+      nsContentUtils::IsPlainTextType(type) ||
+      IsTypeInList(type, gXMLTypes) ||
+      IsTypeInList(type, gSVGTypes) ||
+      IsTypeInList(type, gXMLTypes);
 
     if (knownType) {
       viewSourceChannel->SetContentType(type);
     } else if (IsImageContentType(type.get())) {
       // If it's an image, we want to display it the same way we normally would.
       // Also note the lifetime of "type" allows us to safely use "get()" here.
       contentType = type;
     } else {
       viewSourceChannel->SetContentType(NS_LITERAL_CSTRING(TEXT_PLAIN));
     }
   } else if (aContentType.EqualsLiteral(VIEWSOURCE_CONTENT_TYPE)) {
     aChannel->SetContentType(NS_LITERAL_CSTRING(TEXT_PLAIN));
     contentType = TEXT_PLAIN;
   }
-  // Try html
-  int typeIndex=0;
-  while(gHTMLTypes[typeIndex]) {
-    if (contentType.EqualsASCII(gHTMLTypes[typeIndex++])) {
-      return CreateDocument(aCommand, 
-                            aChannel, aLoadGroup,
-                            aContainer, kHTMLDocumentCID,
-                            aDocListener, aDocViewer);
-    }
+
+  // Try html or plaintext; both use the same document CID
+  if (IsTypeInList(contentType, gHTMLTypes) ||
+      nsContentUtils::IsPlainTextType(contentType)) {
+    return CreateDocument(aCommand,
+                          aChannel, aLoadGroup,
+                          aContainer, kHTMLDocumentCID,
+                          aDocListener, aDocViewer);
   }
 
   // Try XML
-  typeIndex = 0;
-  while(gXMLTypes[typeIndex]) {
-    if (contentType.EqualsASCII(gXMLTypes[typeIndex++])) {
-      return CreateDocument(aCommand, 
-                            aChannel, aLoadGroup,
-                            aContainer, kXMLDocumentCID,
-                            aDocListener, aDocViewer);
-    }
+  if (IsTypeInList(contentType, gXMLTypes)) {
+    return CreateDocument(aCommand,
+                          aChannel, aLoadGroup,
+                          aContainer, kXMLDocumentCID,
+                          aDocListener, aDocViewer);
   }
 
   // Try SVG
-  typeIndex = 0;
-  while(gSVGTypes[typeIndex]) {
-    if (contentType.EqualsASCII(gSVGTypes[typeIndex++])) {
-      return CreateDocument(aCommand,
-                            aChannel, aLoadGroup,
-                            aContainer, kSVGDocumentCID,
-                            aDocListener, aDocViewer);
-    }
+  if (IsTypeInList(contentType, gSVGTypes)) {
+    return CreateDocument(aCommand,
+                          aChannel, aLoadGroup,
+                          aContainer, kSVGDocumentCID,
+                          aDocListener, aDocViewer);
   }
 
   // Try XUL
-  typeIndex = 0;
-  while (gXULTypes[typeIndex]) {
-    if (contentType.EqualsASCII(gXULTypes[typeIndex++])) {
-      if (!MayUseXULXBL(aChannel)) {
-        return NS_ERROR_REMOTE_XUL;
-      }
+  if (IsTypeInList(contentType, gXULTypes)) {
+    if (!MayUseXULXBL(aChannel)) {
+      return NS_ERROR_REMOTE_XUL;
+    }
 
-      return CreateXULDocument(aCommand, aChannel, aLoadGroup, aContainer,
-                               aExtraInfo, aDocListener, aDocViewer);
-    }
+    return CreateXULDocument(aCommand, aChannel, aLoadGroup, aContainer,
+                             aExtraInfo, aDocListener, aDocViewer);
   }
 
   if (mozilla::DecoderTraits::ShouldHandleMediaType(contentType.get())) {
     return CreateDocument(aCommand,
                           aChannel, aLoadGroup,
                           aContainer, kVideoDocumentCID,
                           aDocListener, aDocViewer);
   }