2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-15 17:13:37 -08:00
|
|
|
#ifndef nsFaviconService_h_
|
|
|
|
#define nsFaviconService_h_
|
|
|
|
|
2011-05-20 05:37:02 -07:00
|
|
|
#include "nsIFaviconService.h"
|
|
|
|
#include "mozIAsyncFavicons.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsCOMPtr.h"
|
2011-05-20 05:37:02 -07:00
|
|
|
#include "nsString.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2011-12-15 16:55:22 -08:00
|
|
|
#include "nsTHashtable.h"
|
2009-12-01 05:00:45 -08:00
|
|
|
#include "nsToolkitCompsCID.h"
|
2011-12-15 16:55:22 -08:00
|
|
|
#include "nsURIHashKey.h"
|
|
|
|
#include "nsITimer.h"
|
2011-10-27 02:11:34 -07:00
|
|
|
#include "Database.h"
|
2010-01-08 04:57:49 -08:00
|
|
|
#include "mozilla/storage.h"
|
2012-06-19 20:58:39 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2010-01-08 04:57:49 -08:00
|
|
|
|
2011-12-15 16:55:22 -08:00
|
|
|
#include "AsyncFaviconHelpers.h"
|
|
|
|
|
2009-03-12 14:49:37 -07: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 04:34:42 -07:00
|
|
|
// Most icons will be smaller than this rough estimate of the size of an
|
|
|
|
// uncompressed 16x16 RGBA image of the same dimensions.
|
2012-08-22 08:56:38 -07:00
|
|
|
#define MAX_ICON_FILESIZE(s) ((uint32_t) s*s*4)
|
2010-03-25 04:34:35 -07:00
|
|
|
|
2009-03-12 14:49:37 -07:00
|
|
|
// forward class definitions
|
|
|
|
class mozIStorageStatementCallback;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-12-15 16:55:22 -08: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;
|
|
|
|
};
|
|
|
|
|
2012-06-19 20:58:39 -07:00
|
|
|
class nsFaviconService MOZ_FINAL : public nsIFaviconService
|
|
|
|
, public mozIAsyncFavicons
|
|
|
|
, public nsITimerCallback
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsFaviconService();
|
2009-12-01 05:00:45 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtains the service's object.
|
|
|
|
*/
|
2010-01-08 04:57:49 -08:00
|
|
|
static nsFaviconService* GetSingleton();
|
2009-12-01 05:00:45 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the service's object. This should only be called once.
|
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult Init();
|
|
|
|
|
2011-09-21 11:14:40 -07:00
|
|
|
static nsFaviconService* GetFaviconServiceIfAvailable() {
|
|
|
|
return gFaviconService;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Returns a cached pointer to the favicon service for consumers in the
|
|
|
|
* places directory.
|
|
|
|
*/
|
2010-01-08 04:57:49 -08:00
|
|
|
static nsFaviconService* GetFaviconService()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-12-01 05:00:45 -08:00
|
|
|
if (!gFaviconService) {
|
|
|
|
nsCOMPtr<nsIFaviconService> serv =
|
|
|
|
do_GetService(NS_FAVICONSERVICE_CONTRACTID);
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(serv, nullptr);
|
2009-12-01 05:00:45 -08:00
|
|
|
NS_ASSERTION(gFaviconService, "Should have static instance pointer now");
|
2007-03-22 10:30:00 -07: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);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult OptimizeFaviconImage(const uint8_t* aData, uint32_t aDataLen,
|
2009-10-22 16:10:20 -07:00
|
|
|
const nsACString& aMimeType,
|
|
|
|
nsACString& aNewData, nsACString& aNewMimeType);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t GetOptimizedIconDimension() { return mOptimizedIconDimension; }
|
2008-11-13 22:45:49 -08:00
|
|
|
|
2009-03-12 14:49:37 -07: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 04:57:49 -08:00
|
|
|
nsresult GetFaviconDataAsync(nsIURI* aFaviconURI,
|
|
|
|
mozIStorageStatementCallback* aCallback);
|
2009-03-12 14:49:37 -07:00
|
|
|
|
2011-06-30 13:06:56 -07: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 04:34:47 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIFAVICONSERVICE
|
2011-05-20 05:37:02 -07:00
|
|
|
NS_DECL_MOZIASYNCFAVICONS
|
2011-12-15 16:55:22 -08:00
|
|
|
NS_DECL_NSITIMERCALLBACK
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
~nsFaviconService();
|
|
|
|
|
2011-10-27 02:11:34 -07:00
|
|
|
nsRefPtr<mozilla::places::Database> mDB;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-12-15 16:55:22 -08:00
|
|
|
nsCOMPtr<nsITimer> mExpireUnassociatedIconsTimer;
|
|
|
|
|
2010-01-08 04:57:49 -08:00
|
|
|
static nsFaviconService* gFaviconService;
|
2007-03-22 10:30:00 -07: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 03:04:11 -08: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 05:17:36 -08:00
|
|
|
|
2009-10-22 16:10:20 -07: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.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mOptimizedIconDimension;
|
2009-10-22 16:10:20 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mFailedFaviconSerial;
|
|
|
|
nsDataHashtable<nsCStringHashKey, uint32_t> mFailedFavicons;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-12-15 16:55:22 -08:00
|
|
|
// AsyncFetchAndSetIconForPage needs access to the icon cache
|
|
|
|
friend class mozilla::places::AsyncFetchAndSetIconForPage;
|
|
|
|
friend class mozilla::places::RemoveIconDataCacheEntry;
|
|
|
|
nsTHashtable<UnassociatedIconHashKey> mUnassociatedIcons;
|
|
|
|
|
2010-06-25 14:06:14 -07: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;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#define FAVICON_ANNOTATION_NAME "favicon"
|
2010-11-15 17:13:37 -08:00
|
|
|
|
|
|
|
#endif // nsFaviconService_h_
|