From 7374e2a323ce2f208fbbed05d1f64277d019591f Mon Sep 17 00:00:00 2001 From: "Brian R. Bondy" Date: Wed, 31 Aug 2011 10:28:17 -0400 Subject: [PATCH] Bug 676906 - Added async getFaviconDataForPage in mozIAsyncFavicons. r=mak --- .../components/places/AsyncFaviconHelpers.cpp | 79 ++++++++++++++++++- .../components/places/AsyncFaviconHelpers.h | 47 +++++++++++ .../components/places/nsFaviconService.cpp | 11 +++ 3 files changed, 136 insertions(+), 1 deletion(-) diff --git a/toolkit/components/places/AsyncFaviconHelpers.cpp b/toolkit/components/places/AsyncFaviconHelpers.cpp index 31f0e00236de..4040d232dfc2 100644 --- a/toolkit/components/places/AsyncFaviconHelpers.cpp +++ b/toolkit/components/places/AsyncFaviconHelpers.cpp @@ -859,7 +859,7 @@ AsyncAssociateIconToPage::Run() // static nsresult -AsyncGetFaviconURLForPage::start(nsIURI *aPageURI, +AsyncGetFaviconURLForPage::start(nsIURI* aPageURI, nsCOMPtr& aDBConn, nsIFaviconDataCallback* aCallback) { @@ -927,6 +927,83 @@ AsyncGetFaviconURLForPage::Run() return NS_OK; } +//////////////////////////////////////////////////////////////////////////////// +//// AsyncGetFaviconDataForPage + +// static +nsresult +AsyncGetFaviconDataForPage::start(nsIURI* aPageURI, + nsCOMPtr& aDBConn, + nsIFaviconDataCallback* aCallback) +{ + NS_ENSURE_ARG(aCallback); + NS_ENSURE_ARG(aPageURI); + NS_PRECONDITION(NS_IsMainThread(), + "This should be called on the main thread."); + + nsRefPtr fs = nsFaviconService::GetFaviconService(); + NS_ENSURE_TRUE(fs, NS_ERROR_OUT_OF_MEMORY); + + nsCAutoString pageSpec; + nsresult rv = aPageURI->GetSpec(pageSpec); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr callback = aCallback; + nsRefPtr event = + new AsyncGetFaviconDataForPage(pageSpec, aDBConn, fs, callback); + + nsCOMPtr target = do_GetInterface(aDBConn); + NS_ENSURE_TRUE(target, NS_ERROR_OUT_OF_MEMORY); + rv = target->Dispatch(event, NS_DISPATCH_NORMAL); + NS_ENSURE_SUCCESS(rv, rv); + return NS_OK; +} + +AsyncGetFaviconDataForPage::AsyncGetFaviconDataForPage(const nsACString& aPageSpec, + nsCOMPtr& aDBConn, + nsRefPtr& aFaviconSvc, + nsCOMPtr& aCallback) + : AsyncFaviconHelperBase(aDBConn, aFaviconSvc, aCallback) +{ + mPageSpec.Assign(aPageSpec); +} + +AsyncGetFaviconDataForPage::~AsyncGetFaviconDataForPage() +{ +} + +NS_IMETHODIMP +AsyncGetFaviconDataForPage::Run() +{ + NS_PRECONDITION(!NS_IsMainThread(), + "This should not be called on the main thread."); + + nsCAutoString iconSpec; + nsresult rv = FetchIconURL(mFaviconSvc->mSyncStatements, + mPageSpec, iconSpec); + NS_ENSURE_SUCCESS(rv, rv); + + if (!iconSpec.Length()) { + return NS_ERROR_NOT_AVAILABLE; + } + + IconData iconData; + iconData.spec.Assign(iconSpec); + + PageData pageData; + pageData.spec.Assign(mPageSpec); + + rv = FetchIconInfo(mFaviconSvc->mSyncStatements, iconData); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr event = + new NotifyIconObservers(iconData, pageData, mDBConn, + mFaviconSvc, mCallback); + rv = NS_DispatchToMainThread(event); + NS_ENSURE_SUCCESS(rv, rv); + return NS_OK; +} + //////////////////////////////////////////////////////////////////////////////// //// NotifyIconObservers diff --git a/toolkit/components/places/AsyncFaviconHelpers.h b/toolkit/components/places/AsyncFaviconHelpers.h index ccefd72d5360..169747ec8a33 100644 --- a/toolkit/components/places/AsyncFaviconHelpers.h +++ b/toolkit/components/places/AsyncFaviconHelpers.h @@ -314,6 +314,53 @@ private: nsCString mPageSpec; }; + +/** + * Asynchronously tries to get the URL and data of a page's favicon. + * If this succeeds, notifies the given observer. + */ +class AsyncGetFaviconDataForPage : public AsyncFaviconHelperBase +{ +public: + NS_DECL_NSIRUNNABLE + + /** + * Creates the event and dispatches it to the I/O thread. + * + * @param aPageURI + * URL of the page whose favicon URL and data we're fetching + * @param aDBConn + * database connection to use + * @param aCallback + * function to be called once the URL and data is retrieved from the database + */ + static nsresult start(nsIURI* aPageURI, + nsCOMPtr& aDBConn, + nsIFaviconDataCallback* aCallback); + + /** + * Constructor. + * + * @param aPageSpec + * URL of the page whose favicon URL and data we're fetching + * @param aDBConn + * database connection to use + * @param aFaviconSvc + * the favicon service to query + * @param aCallback + * function to be called once the URL is retrieved from the database + */ + AsyncGetFaviconDataForPage(const nsACString& aPageSpec, + nsCOMPtr& aDBConn, + nsRefPtr& aFaviconSvc, + nsCOMPtr& aCallback); + + virtual ~AsyncGetFaviconDataForPage(); + +private: + nsCString mPageSpec; +}; + /** * Notifies the icon change to favicon observers. */ diff --git a/toolkit/components/places/nsFaviconService.cpp b/toolkit/components/places/nsFaviconService.cpp index cbc8e1220986..0d620b23e577 100644 --- a/toolkit/components/places/nsFaviconService.cpp +++ b/toolkit/components/places/nsFaviconService.cpp @@ -718,6 +718,17 @@ nsFaviconService::GetFaviconURLForPage(nsIURI *aPageURI, return NS_OK; } +NS_IMETHODIMP +nsFaviconService::GetFaviconDataForPage(nsIURI* aPageURI, + nsIFaviconDataCallback* aCallback) +{ + NS_ENSURE_ARG(aPageURI); + NS_ENSURE_ARG(aCallback); + + nsresult rv = AsyncGetFaviconDataForPage::start(aPageURI, mDBConn, aCallback); + NS_ENSURE_SUCCESS(rv, rv); + return NS_OK; +} NS_IMETHODIMP nsFaviconService::GetFaviconImageForPage(nsIURI* aPageURI, nsIURI** _retval)