1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/nsFaviconService.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,160 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsFaviconService_h_ 1.10 +#define nsFaviconService_h_ 1.11 + 1.12 +#include "nsIFaviconService.h" 1.13 +#include "mozIAsyncFavicons.h" 1.14 + 1.15 +#include "nsCOMPtr.h" 1.16 +#include "nsString.h" 1.17 +#include "nsDataHashtable.h" 1.18 +#include "nsServiceManagerUtils.h" 1.19 +#include "nsTHashtable.h" 1.20 +#include "nsToolkitCompsCID.h" 1.21 +#include "nsURIHashKey.h" 1.22 +#include "nsITimer.h" 1.23 +#include "Database.h" 1.24 +#include "mozilla/storage.h" 1.25 +#include "mozilla/Attributes.h" 1.26 + 1.27 +#include "AsyncFaviconHelpers.h" 1.28 + 1.29 +// Favicons bigger than this size should not be saved to the db to avoid 1.30 +// bloating it with large image blobs. 1.31 +// This still allows us to accept a favicon even if we cannot optimize it. 1.32 +#define MAX_FAVICON_SIZE 10240 1.33 + 1.34 +// Most icons will be smaller than this rough estimate of the size of an 1.35 +// uncompressed 16x16 RGBA image of the same dimensions. 1.36 +#define MAX_ICON_FILESIZE(s) ((uint32_t) s*s*4) 1.37 + 1.38 +// forward class definitions 1.39 +class mozIStorageStatementCallback; 1.40 + 1.41 +class UnassociatedIconHashKey : public nsURIHashKey 1.42 +{ 1.43 +public: 1.44 + UnassociatedIconHashKey(const nsIURI* aURI) 1.45 + : nsURIHashKey(aURI) 1.46 + { 1.47 + } 1.48 + UnassociatedIconHashKey(const UnassociatedIconHashKey& aOther) 1.49 + : nsURIHashKey(aOther) 1.50 + { 1.51 + NS_NOTREACHED("Do not call me!"); 1.52 + } 1.53 + mozilla::places::IconData iconData; 1.54 + PRTime created; 1.55 +}; 1.56 + 1.57 +class nsFaviconService MOZ_FINAL : public nsIFaviconService 1.58 + , public mozIAsyncFavicons 1.59 + , public nsITimerCallback 1.60 +{ 1.61 +public: 1.62 + nsFaviconService(); 1.63 + 1.64 + /** 1.65 + * Obtains the service's object. 1.66 + */ 1.67 + static already_AddRefed<nsFaviconService> GetSingleton(); 1.68 + 1.69 + /** 1.70 + * Initializes the service's object. This should only be called once. 1.71 + */ 1.72 + nsresult Init(); 1.73 + 1.74 + /** 1.75 + * Returns a cached pointer to the favicon service for consumers in the 1.76 + * places directory. 1.77 + */ 1.78 + static nsFaviconService* GetFaviconService() 1.79 + { 1.80 + if (!gFaviconService) { 1.81 + nsCOMPtr<nsIFaviconService> serv = 1.82 + do_GetService(NS_FAVICONSERVICE_CONTRACTID); 1.83 + NS_ENSURE_TRUE(serv, nullptr); 1.84 + NS_ASSERTION(gFaviconService, "Should have static instance pointer now"); 1.85 + } 1.86 + return gFaviconService; 1.87 + } 1.88 + 1.89 + // addition to API for strings to prevent excessive parsing of URIs 1.90 + nsresult GetFaviconLinkForIconString(const nsCString& aIcon, nsIURI** aOutput); 1.91 + void GetFaviconSpecForIconString(const nsCString& aIcon, nsACString& aOutput); 1.92 + 1.93 + nsresult OptimizeFaviconImage(const uint8_t* aData, uint32_t aDataLen, 1.94 + const nsACString& aMimeType, 1.95 + nsACString& aNewData, nsACString& aNewMimeType); 1.96 + int32_t GetOptimizedIconDimension() { return mOptimizedIconDimension; } 1.97 + 1.98 + /** 1.99 + * Obtains the favicon data asynchronously. 1.100 + * 1.101 + * @param aFaviconURI 1.102 + * The URI representing the favicon we are looking for. 1.103 + * @param aCallback 1.104 + * The callback where results or errors will be dispatch to. In the 1.105 + * returned result, the favicon binary data will be at index 0, and the 1.106 + * mime type will be at index 1. 1.107 + */ 1.108 + nsresult GetFaviconDataAsync(nsIURI* aFaviconURI, 1.109 + mozIStorageStatementCallback* aCallback); 1.110 + 1.111 + /** 1.112 + * Call to send out favicon changed notifications. Should only be called 1.113 + * when there is data loaded for the favicon. 1.114 + * @param aPageURI 1.115 + * The URI of the page to notify about. 1.116 + * @param aFaviconURI 1.117 + * The moz-anno:favicon URI of the icon. 1.118 + * @param aGUID 1.119 + * The unique ID associated with the page. 1.120 + */ 1.121 + void SendFaviconNotifications(nsIURI* aPageURI, nsIURI* aFaviconURI, 1.122 + const nsACString& aGUID); 1.123 + 1.124 + NS_DECL_ISUPPORTS 1.125 + NS_DECL_NSIFAVICONSERVICE 1.126 + NS_DECL_MOZIASYNCFAVICONS 1.127 + NS_DECL_NSITIMERCALLBACK 1.128 + 1.129 +private: 1.130 + ~nsFaviconService(); 1.131 + 1.132 + nsRefPtr<mozilla::places::Database> mDB; 1.133 + 1.134 + nsCOMPtr<nsITimer> mExpireUnassociatedIconsTimer; 1.135 + 1.136 + static nsFaviconService* gFaviconService; 1.137 + 1.138 + /** 1.139 + * A cached URI for the default icon. We return this a lot, and don't want to 1.140 + * re-parse and normalize our unchanging string many times. Important: do 1.141 + * not return this directly; use Clone() since callers may change the object 1.142 + * they get back. May be null, in which case it needs initialization. 1.143 + */ 1.144 + nsCOMPtr<nsIURI> mDefaultIcon; 1.145 + 1.146 + // The target dimension, in pixels, for favicons we optimize. 1.147 + // If we find images that are as large or larger than an uncompressed RGBA 1.148 + // image of this size (mOptimizedIconDimension*mOptimizedIconDimension*4), 1.149 + // we will try to optimize it. 1.150 + int32_t mOptimizedIconDimension; 1.151 + 1.152 + uint32_t mFailedFaviconSerial; 1.153 + nsDataHashtable<nsCStringHashKey, uint32_t> mFailedFavicons; 1.154 + 1.155 + // AsyncFetchAndSetIconForPage needs access to the icon cache 1.156 + friend class mozilla::places::AsyncFetchAndSetIconForPage; 1.157 + friend class mozilla::places::RemoveIconDataCacheEntry; 1.158 + nsTHashtable<UnassociatedIconHashKey> mUnassociatedIcons; 1.159 +}; 1.160 + 1.161 +#define FAVICON_ANNOTATION_NAME "favicon" 1.162 + 1.163 +#endif // nsFaviconService_h_