Bug 306471 part 1 - Some code style cleanup for nsExternalHelperAppService::GetTypeFromExtension. r=bz draft
authorXidorn Quan <me@upsuper.org>
Fri, 12 Aug 2016 20:56:23 +1000
changeset 404738 825308563f4d1ebdb3e672d088975df30bf89392
parent 402944 cf06fbc831754e54c6abb71d3136597488a530e0
child 404739 6841d74e7e8ad39377a13c1f87e35614a2d333a1
child 404740 b223c630266294dc157e88d1a6090a1c99dd8746
push id27278
push usermozilla@upsuper.org
push dateTue, 23 Aug 2016 23:33:13 +0000
reviewersbz
bugs306471
milestone51.0a1
Bug 306471 part 1 - Some code style cleanup for nsExternalHelperAppService::GetTypeFromExtension. r=bz MozReview-Commit-ID: 3N37Kd2fNjS
uriloader/exthandler/nsExternalHelperAppService.cpp
--- a/uriloader/exthandler/nsExternalHelperAppService.cpp
+++ b/uriloader/exthandler/nsExternalHelperAppService.cpp
@@ -2717,83 +2717,89 @@ NS_IMETHODIMP nsExternalHelperAppService
     nsAutoCString ext;
     (*_retval)->GetPrimaryExtension(ext);
     LOG(("MIME Info Summary: Type '%s', Primary Ext '%s'\n", type.get(), ext.get()));
   }
 
   return NS_OK;
 }
 
-NS_IMETHODIMP nsExternalHelperAppService::GetTypeFromExtension(const nsACString& aFileExt, nsACString& aContentType) 
+NS_IMETHODIMP
+nsExternalHelperAppService::GetTypeFromExtension(const nsACString& aFileExt,
+                                                 nsACString& aContentType)
 {
   // OK. We want to try the following sources of mimetype information, in this order:
   // 1. defaultMimeEntries array
   // 2. User-set preferences (managed by the handler service)
   // 3. OS-provided information
   // 4. our "extras" array
   // 5. Information from plugins
   // 6. The "ext-to-type-mapping" category
 
   // Early return if called with an empty extension parameter
-  if (aFileExt.IsEmpty())
+  if (aFileExt.IsEmpty()) {
     return NS_ERROR_NOT_AVAILABLE;
-
-  nsresult rv = NS_OK;
+  }
+
   // First of all, check our default entries
-  for (size_t i = 0; i < ArrayLength(defaultMimeEntries); i++)
-  {
-    if (aFileExt.LowerCaseEqualsASCII(defaultMimeEntries[i].mFileExtension)) {
-      aContentType = defaultMimeEntries[i].mMimeType;
-      return rv;
+  for (auto& entry : defaultMimeEntries) {
+    if (aFileExt.LowerCaseEqualsASCII(entry.mFileExtension)) {
+      aContentType = entry.mMimeType;
+      return NS_OK;
     }
   }
 
   // Check user-set prefs
   nsCOMPtr<nsIHandlerService> handlerSvc = do_GetService(NS_HANDLERSERVICE_CONTRACTID);
-  if (handlerSvc)
-    rv = handlerSvc->GetTypeFromExtension(aFileExt, aContentType);
-  if (NS_SUCCEEDED(rv) && !aContentType.IsEmpty())
-    return NS_OK;
+  if (handlerSvc) {
+    nsresult rv = handlerSvc->GetTypeFromExtension(aFileExt, aContentType);
+    if (NS_SUCCEEDED(rv) && !aContentType.IsEmpty()) {
+      return NS_OK;
+    }
+  }
 
   // Ask OS.
   bool found = false;
   nsCOMPtr<nsIMIMEInfo> mi = GetMIMEInfoFromOS(EmptyCString(), aFileExt, &found);
-  if (mi && found)
+  if (mi && found) {
     return mi->GetMIMEType(aContentType);
+  }
 
   // Check extras array.
   found = GetTypeFromExtras(aFileExt, aContentType);
-  if (found)
+  if (found) {
     return NS_OK;
+  }
 
   // Try the plugins
   RefPtr<nsPluginHost> pluginHost = nsPluginHost::GetInst();
   if (pluginHost &&
       pluginHost->HavePluginForExtension(aFileExt, aContentType)) {
     return NS_OK;
   }
 
-  rv = NS_OK;
   // Let's see if an extension added something
-  nsCOMPtr<nsICategoryManager> catMan(do_GetService("@mozilla.org/categorymanager;1"));
+  nsCOMPtr<nsICategoryManager> catMan(
+    do_GetService("@mozilla.org/categorymanager;1"));
   if (catMan) {
     // The extension in the category entry is always stored as lowercase
     nsAutoCString lowercaseFileExt(aFileExt);
     ToLowerCase(lowercaseFileExt);
     // Read the MIME type from the category entry, if available
     nsXPIDLCString type;
-    rv = catMan->GetCategoryEntry("ext-to-type-mapping", lowercaseFileExt.get(),
-                                  getter_Copies(type));
-    aContentType = type;
+    nsresult rv = catMan->GetCategoryEntry("ext-to-type-mapping",
+                                           lowercaseFileExt.get(),
+                                           getter_Copies(type));
+    if (NS_SUCCEEDED(rv)) {
+      aContentType = type;
+      return NS_OK;
+    }
   }
-  else {
-    rv = NS_ERROR_NOT_AVAILABLE;
-  }
-
-  return rv;
+
+  return NS_ERROR_NOT_AVAILABLE;
 }
 
 NS_IMETHODIMP nsExternalHelperAppService::GetPrimaryExtension(const nsACString& aMIMEType, const nsACString& aFileExt, nsACString& _retval)
 {
   NS_ENSURE_ARG(!aMIMEType.IsEmpty());
 
   nsCOMPtr<nsIMIMEInfo> mi;
   nsresult rv = GetFromTypeAndExtension(aMIMEType, aFileExt, getter_AddRefs(mi));