Bug 813438 - Part 4: Rewrite AsyncFaviconHelper to use NS_SniffContent; r=bzbarsky
authorEhsan Akhgari <ehsan@mozilla.com>
Tue, 20 Nov 2012 19:41:35 -0500
changeset 114043 2e3dfbc9d7988d9dc551047301ed227b3d5b8693
parent 114042 1002aa671831f4b0b8fda7fa596807e9828cd8f9
child 114044 a852c3f0e2fda008678c7c8b0cbdc04c06178abb
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 4: Rewrite AsyncFaviconHelper to use NS_SniffContent; r=bzbarsky
toolkit/components/places/AsyncFaviconHelpers.cpp
--- a/toolkit/components/places/AsyncFaviconHelpers.cpp
+++ b/toolkit/components/places/AsyncFaviconHelpers.cpp
@@ -1,35 +1,32 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "AsyncFaviconHelpers.h"
 
-#include "nsIContentSniffer.h"
 #include "nsICacheService.h"
 #include "nsICacheVisitor.h"
 #include "nsICachingChannel.h"
 #include "nsIAsyncVerifyRedirectCallback.h"
 
 #include "nsNavHistory.h"
 #include "nsFaviconService.h"
 #include "mozilla/storage.h"
 #include "nsNetUtil.h"
 #include "nsPrintfCString.h"
 #include "nsStreamUtils.h"
 #include "nsIPrivateBrowsingChannel.h"
 #if !(defined(MOZ_PER_WINDOW_PRIVATE_BROWSING)) && defined(DEBUG)
 #include "nsIPrivateBrowsingService.h"
 #endif
 
-#define CONTENT_SNIFFING_SERVICES "content-sniffing-services"
-
 using namespace mozilla::places;
 using namespace mozilla::storage;
 
 namespace mozilla {
 namespace places {
 
 namespace {
 
@@ -284,65 +281,16 @@ FetchIconURL(nsRefPtr<Database>& aDB,
     rv = stmt->GetUTF8String(0, aIconSpec);
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
   return NS_OK;
 }
 
 /**
- * Tries to guess the mimeType from icon data.
- *
- * @param aRequest
- *        The network request object.
- * @param aData
- *        Data for this icon.
- * @param _mimeType
- *        The guessed mime-type or empty string if a valid one can't be found.
- */
-nsresult
-SniffMimeTypeForIconData(nsIRequest* aRequest,
-                         const nsCString& aData,
-                         nsCString& _mimeType)
-{
-  NS_PRECONDITION(NS_IsMainThread(),
-                  "This should be called on the main thread");
-
-  nsCOMPtr<nsICategoryManager> categoryManager =
-    do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
-  NS_ENSURE_TRUE(categoryManager, NS_ERROR_OUT_OF_MEMORY);
-  nsCOMPtr<nsISimpleEnumerator> sniffers;
-  nsresult rv = categoryManager->EnumerateCategory(CONTENT_SNIFFING_SERVICES,
-                                                   getter_AddRefs(sniffers));
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  bool hasMore = false;
-  while (_mimeType.IsEmpty() &&
-         NS_SUCCEEDED(sniffers->HasMoreElements(&hasMore)) &&
-         hasMore) {
-    nsCOMPtr<nsISupports> snifferCIDSupports;
-    rv = sniffers->GetNext(getter_AddRefs(snifferCIDSupports));
-    NS_ENSURE_SUCCESS(rv, rv);
-    nsCOMPtr<nsISupportsCString> snifferCIDSupportsCString =
-      do_QueryInterface(snifferCIDSupports);
-    NS_ENSURE_STATE(snifferCIDSupports);
-    nsAutoCString snifferCID;
-    rv = snifferCIDSupportsCString->GetData(snifferCID);
-    NS_ENSURE_SUCCESS(rv, rv);
-    nsCOMPtr<nsIContentSniffer> sniffer = do_GetService(snifferCID.get());
-    NS_ENSURE_STATE(sniffer);
-
-     // Ignore errors: we'll try the next sniffer.
-    (void)sniffer->GetMIMETypeFromContent(aRequest, TO_INTBUFFER(aData),
-                                          aData.Length(), _mimeType);
-  }
-  return NS_OK;
-}
-
-/**
  * Tries to compute the expiration time for a icon from the channel.
  *
  * @param aChannel
  *        The network channel used to fetch the icon.
  * @return a valid expiration value for the fetched icon.
  */
 PRTime
 GetExpirationTimeFromChannel(nsIChannel* aChannel)
@@ -691,28 +639,31 @@ AsyncFetchAndSetIconFromNetwork::OnStopR
                                                nsISupports* aContext,
                                                nsresult aStatusCode)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   nsFaviconService* favicons = nsFaviconService::GetFaviconService();
   NS_ENSURE_STATE(favicons);
 
+  nsresult rv;
+
   // If fetching the icon failed, add it to the failed cache.
   if (NS_FAILED(aStatusCode) || mIcon.data.Length() == 0) {
     nsCOMPtr<nsIURI> iconURI;
-    nsresult rv = NS_NewURI(getter_AddRefs(iconURI), mIcon.spec);
+    rv = NS_NewURI(getter_AddRefs(iconURI), mIcon.spec);
     NS_ENSURE_SUCCESS(rv, rv);
     rv = favicons->AddFailedFavicon(iconURI);
     NS_ENSURE_SUCCESS(rv, rv);
     return NS_OK;
   }
 
-  nsresult rv = SniffMimeTypeForIconData(aRequest, mIcon.data, mIcon.mimeType);
-  NS_ENSURE_SUCCESS(rv, rv);
+  NS_SniffContent(NS_DATA_SNIFFER_CATEGORY, aRequest,
+                  TO_INTBUFFER(mIcon.data), mIcon.data.Length(),
+                  mIcon.mimeType);
 
   // If the icon does not have a valid MIME type, add it to the failed cache.
   if (mIcon.mimeType.IsEmpty()) {
     nsCOMPtr<nsIURI> iconURI;
     rv = NS_NewURI(getter_AddRefs(iconURI), mIcon.spec);
     NS_ENSURE_SUCCESS(rv, rv);
     rv = favicons->AddFailedFavicon(iconURI);
     NS_ENSURE_SUCCESS(rv, rv);