2006-07-18 20:55:18 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2006-07-18 20:55:18 +04:00
|
|
|
|
2010-11-16 04:13:37 +03:00
|
|
|
#ifndef nsFaviconService_h_
|
|
|
|
#define nsFaviconService_h_
|
|
|
|
|
2011-05-20 16:37:02 +04:00
|
|
|
#include "nsIFaviconService.h"
|
|
|
|
#include "mozIAsyncFavicons.h"
|
|
|
|
|
2006-07-18 20:55:18 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2011-05-20 16:37:02 +04:00
|
|
|
#include "nsString.h"
|
2006-07-18 20:56:03 +04:00
|
|
|
#include "nsDataHashtable.h"
|
2006-07-18 20:55:18 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2011-12-16 04:55:22 +04:00
|
|
|
#include "nsTHashtable.h"
|
2009-12-01 16:00:45 +03:00
|
|
|
#include "nsToolkitCompsCID.h"
|
2011-12-16 04:55:22 +04:00
|
|
|
#include "nsURIHashKey.h"
|
|
|
|
#include "nsITimer.h"
|
2011-10-27 13:11:34 +04:00
|
|
|
#include "Database.h"
|
2010-01-08 15:57:49 +03:00
|
|
|
#include "mozilla/storage.h"
|
|
|
|
|
2011-12-16 04:55:22 +04:00
|
|
|
#include "AsyncFaviconHelpers.h"
|
|
|
|
|
2009-03-13 00:49:37 +03:00
|
|
|
// Favicons bigger than this size should not be saved to the db to avoid
|
|
|
|
// bloating it with large image blobs.
|
|
|
|
// This still allows us to accept a favicon even if we cannot optimize it.
|
|
|
|
#define MAX_FAVICON_SIZE 10240
|
|
|
|
|
2010-03-25 14:34:42 +03:00
|
|
|
// Most icons will be smaller than this rough estimate of the size of an
|
|
|
|
// uncompressed 16x16 RGBA image of the same dimensions.
|
|
|
|
#define MAX_ICON_FILESIZE(s) ((PRUint32) s*s*4)
|
2010-03-25 14:34:35 +03:00
|
|
|
|
2009-03-13 00:49:37 +03:00
|
|
|
// forward class definitions
|
|
|
|
class mozIStorageStatementCallback;
|
2006-07-18 20:56:03 +04:00
|
|
|
|
2011-12-16 04:55:22 +04:00
|
|
|
class UnassociatedIconHashKey : public nsURIHashKey
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UnassociatedIconHashKey(const nsIURI* aURI)
|
|
|
|
: nsURIHashKey(aURI)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
UnassociatedIconHashKey(const UnassociatedIconHashKey& aOther)
|
|
|
|
: nsURIHashKey(aOther)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("Do not call me!");
|
|
|
|
}
|
|
|
|
mozilla::places::IconData iconData;
|
|
|
|
PRTime created;
|
|
|
|
};
|
|
|
|
|
2006-07-18 20:55:18 +04:00
|
|
|
class nsFaviconService : public nsIFaviconService
|
2011-05-20 16:37:02 +04:00
|
|
|
, public mozIAsyncFavicons
|
2011-12-16 04:55:22 +04:00
|
|
|
, public nsITimerCallback
|
2006-07-18 20:55:18 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsFaviconService();
|
2009-12-01 16:00:45 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtains the service's object.
|
|
|
|
*/
|
2010-01-08 15:57:49 +03:00
|
|
|
static nsFaviconService* GetSingleton();
|
2009-12-01 16:00:45 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the service's object. This should only be called once.
|
|
|
|
*/
|
2006-07-18 20:55:18 +04:00
|
|
|
nsresult Init();
|
|
|
|
|
2011-09-21 22:14:40 +04:00
|
|
|
static nsFaviconService* GetFaviconServiceIfAvailable() {
|
|
|
|
return gFaviconService;
|
|
|
|
}
|
|
|
|
|
2006-07-18 20:55:18 +04:00
|
|
|
/**
|
|
|
|
* Returns a cached pointer to the favicon service for consumers in the
|
|
|
|
* places directory.
|
|
|
|
*/
|
2010-01-08 15:57:49 +03:00
|
|
|
static nsFaviconService* GetFaviconService()
|
2006-07-18 20:55:18 +04:00
|
|
|
{
|
2009-12-01 16:00:45 +03:00
|
|
|
if (!gFaviconService) {
|
|
|
|
nsCOMPtr<nsIFaviconService> serv =
|
|
|
|
do_GetService(NS_FAVICONSERVICE_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(serv, nsnull);
|
|
|
|
NS_ASSERTION(gFaviconService, "Should have static instance pointer now");
|
2006-07-18 20:55:18 +04:00
|
|
|
}
|
|
|
|
return gFaviconService;
|
|
|
|
}
|
|
|
|
|
|
|
|
// addition to API for strings to prevent excessive parsing of URIs
|
|
|
|
nsresult GetFaviconLinkForIconString(const nsCString& aIcon, nsIURI** aOutput);
|
|
|
|
void GetFaviconSpecForIconString(const nsCString& aIcon, nsACString& aOutput);
|
|
|
|
|
2009-10-23 03:10:20 +04:00
|
|
|
nsresult OptimizeFaviconImage(const PRUint8* aData, PRUint32 aDataLen,
|
|
|
|
const nsACString& aMimeType,
|
|
|
|
nsACString& aNewData, nsACString& aNewMimeType);
|
2010-03-25 14:34:42 +03:00
|
|
|
PRInt32 GetOptimizedIconDimension() { return mOptimizedIconDimension; }
|
2008-11-14 09:45:49 +03:00
|
|
|
|
2009-03-13 00:49:37 +03:00
|
|
|
/**
|
|
|
|
* Obtains the favicon data asynchronously.
|
|
|
|
*
|
|
|
|
* @param aFaviconURI
|
|
|
|
* The URI representing the favicon we are looking for.
|
|
|
|
* @param aCallback
|
|
|
|
* The callback where results or errors will be dispatch to. In the
|
|
|
|
* returned result, the favicon binary data will be at index 0, and the
|
|
|
|
* mime type will be at index 1.
|
|
|
|
*/
|
2010-01-08 15:57:49 +03:00
|
|
|
nsresult GetFaviconDataAsync(nsIURI* aFaviconURI,
|
|
|
|
mozIStorageStatementCallback* aCallback);
|
2009-03-13 00:49:37 +03:00
|
|
|
|
2011-07-01 00:06:56 +04:00
|
|
|
/**
|
|
|
|
* Call to send out favicon changed notifications. Should only be called
|
|
|
|
* when there is data loaded for the favicon.
|
|
|
|
* @param aPageURI
|
|
|
|
* The URI of the page to notify about.
|
|
|
|
* @param aFaviconURI
|
|
|
|
* The moz-anno:favicon URI of the icon.
|
|
|
|
* @param aGUID
|
|
|
|
* The unique ID associated with the page.
|
|
|
|
*/
|
|
|
|
void SendFaviconNotifications(nsIURI* aPageURI, nsIURI* aFaviconURI,
|
|
|
|
const nsACString& aGUID);
|
2010-03-25 14:34:47 +03:00
|
|
|
|
2006-07-18 20:55:18 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIFAVICONSERVICE
|
2011-05-20 16:37:02 +04:00
|
|
|
NS_DECL_MOZIASYNCFAVICONS
|
2011-12-16 04:55:22 +04:00
|
|
|
NS_DECL_NSITIMERCALLBACK
|
2006-07-18 20:55:18 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
~nsFaviconService();
|
|
|
|
|
2011-10-27 13:11:34 +04:00
|
|
|
nsRefPtr<mozilla::places::Database> mDB;
|
2006-07-18 20:55:18 +04:00
|
|
|
|
2011-12-16 04:55:22 +04:00
|
|
|
nsCOMPtr<nsITimer> mExpireUnassociatedIconsTimer;
|
|
|
|
|
2010-01-08 15:57:49 +03:00
|
|
|
static nsFaviconService* gFaviconService;
|
2006-07-18 20:55:18 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A cached URI for the default icon. We return this a lot, and don't want to
|
|
|
|
* re-parse and normalize our unchanging string many times. Important: do
|
|
|
|
* not return this directly; use Clone() since callers may change the object
|
|
|
|
* they get back. May be null, in which case it needs initialization.
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsIURI> mDefaultIcon;
|
|
|
|
|
2009-11-11 14:04:11 +03:00
|
|
|
// Set to true during favicons expiration, addition of new favicons won't be
|
|
|
|
// allowed till expiration has finished since those should then be expired.
|
|
|
|
bool mFaviconsExpirationRunning;
|
2009-02-28 16:17:36 +03:00
|
|
|
|
2009-10-23 03:10:20 +04:00
|
|
|
// The target dimension, in pixels, for favicons we optimize.
|
|
|
|
// If we find images that are as large or larger than an uncompressed RGBA
|
|
|
|
// image of this size (mOptimizedIconDimension*mOptimizedIconDimension*4),
|
|
|
|
// we will try to optimize it.
|
|
|
|
PRInt32 mOptimizedIconDimension;
|
|
|
|
|
2006-07-18 20:56:03 +04:00
|
|
|
PRUint32 mFailedFaviconSerial;
|
|
|
|
nsDataHashtable<nsCStringHashKey, PRUint32> mFailedFavicons;
|
|
|
|
|
2011-12-16 04:55:22 +04:00
|
|
|
// AsyncFetchAndSetIconForPage needs access to the icon cache
|
|
|
|
friend class mozilla::places::AsyncFetchAndSetIconForPage;
|
|
|
|
friend class mozilla::places::RemoveIconDataCacheEntry;
|
|
|
|
nsTHashtable<UnassociatedIconHashKey> mUnassociatedIcons;
|
|
|
|
|
2010-06-26 01:06:14 +04:00
|
|
|
// Caches the content of the default favicon if it's not already cached and
|
|
|
|
// copies it into byteStr.
|
|
|
|
nsresult GetDefaultFaviconData(nsCString& byteStr);
|
|
|
|
|
|
|
|
// A string of bytes caching the default favicon's content. Empty if not yet
|
|
|
|
// cached. Rather than accessing this directly, use GetDefaultFaviconData.
|
|
|
|
nsCString mDefaultFaviconData;
|
2006-07-18 20:55:18 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#define FAVICON_ANNOTATION_NAME "favicon"
|
2010-11-16 04:13:37 +03:00
|
|
|
|
|
|
|
#endif // nsFaviconService_h_
|