toolkit/components/places/nsFaviconService.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef nsFaviconService_h_
michael@0 7 #define nsFaviconService_h_
michael@0 8
michael@0 9 #include "nsIFaviconService.h"
michael@0 10 #include "mozIAsyncFavicons.h"
michael@0 11
michael@0 12 #include "nsCOMPtr.h"
michael@0 13 #include "nsString.h"
michael@0 14 #include "nsDataHashtable.h"
michael@0 15 #include "nsServiceManagerUtils.h"
michael@0 16 #include "nsTHashtable.h"
michael@0 17 #include "nsToolkitCompsCID.h"
michael@0 18 #include "nsURIHashKey.h"
michael@0 19 #include "nsITimer.h"
michael@0 20 #include "Database.h"
michael@0 21 #include "mozilla/storage.h"
michael@0 22 #include "mozilla/Attributes.h"
michael@0 23
michael@0 24 #include "AsyncFaviconHelpers.h"
michael@0 25
michael@0 26 // Favicons bigger than this size should not be saved to the db to avoid
michael@0 27 // bloating it with large image blobs.
michael@0 28 // This still allows us to accept a favicon even if we cannot optimize it.
michael@0 29 #define MAX_FAVICON_SIZE 10240
michael@0 30
michael@0 31 // Most icons will be smaller than this rough estimate of the size of an
michael@0 32 // uncompressed 16x16 RGBA image of the same dimensions.
michael@0 33 #define MAX_ICON_FILESIZE(s) ((uint32_t) s*s*4)
michael@0 34
michael@0 35 // forward class definitions
michael@0 36 class mozIStorageStatementCallback;
michael@0 37
michael@0 38 class UnassociatedIconHashKey : public nsURIHashKey
michael@0 39 {
michael@0 40 public:
michael@0 41 UnassociatedIconHashKey(const nsIURI* aURI)
michael@0 42 : nsURIHashKey(aURI)
michael@0 43 {
michael@0 44 }
michael@0 45 UnassociatedIconHashKey(const UnassociatedIconHashKey& aOther)
michael@0 46 : nsURIHashKey(aOther)
michael@0 47 {
michael@0 48 NS_NOTREACHED("Do not call me!");
michael@0 49 }
michael@0 50 mozilla::places::IconData iconData;
michael@0 51 PRTime created;
michael@0 52 };
michael@0 53
michael@0 54 class nsFaviconService MOZ_FINAL : public nsIFaviconService
michael@0 55 , public mozIAsyncFavicons
michael@0 56 , public nsITimerCallback
michael@0 57 {
michael@0 58 public:
michael@0 59 nsFaviconService();
michael@0 60
michael@0 61 /**
michael@0 62 * Obtains the service's object.
michael@0 63 */
michael@0 64 static already_AddRefed<nsFaviconService> GetSingleton();
michael@0 65
michael@0 66 /**
michael@0 67 * Initializes the service's object. This should only be called once.
michael@0 68 */
michael@0 69 nsresult Init();
michael@0 70
michael@0 71 /**
michael@0 72 * Returns a cached pointer to the favicon service for consumers in the
michael@0 73 * places directory.
michael@0 74 */
michael@0 75 static nsFaviconService* GetFaviconService()
michael@0 76 {
michael@0 77 if (!gFaviconService) {
michael@0 78 nsCOMPtr<nsIFaviconService> serv =
michael@0 79 do_GetService(NS_FAVICONSERVICE_CONTRACTID);
michael@0 80 NS_ENSURE_TRUE(serv, nullptr);
michael@0 81 NS_ASSERTION(gFaviconService, "Should have static instance pointer now");
michael@0 82 }
michael@0 83 return gFaviconService;
michael@0 84 }
michael@0 85
michael@0 86 // addition to API for strings to prevent excessive parsing of URIs
michael@0 87 nsresult GetFaviconLinkForIconString(const nsCString& aIcon, nsIURI** aOutput);
michael@0 88 void GetFaviconSpecForIconString(const nsCString& aIcon, nsACString& aOutput);
michael@0 89
michael@0 90 nsresult OptimizeFaviconImage(const uint8_t* aData, uint32_t aDataLen,
michael@0 91 const nsACString& aMimeType,
michael@0 92 nsACString& aNewData, nsACString& aNewMimeType);
michael@0 93 int32_t GetOptimizedIconDimension() { return mOptimizedIconDimension; }
michael@0 94
michael@0 95 /**
michael@0 96 * Obtains the favicon data asynchronously.
michael@0 97 *
michael@0 98 * @param aFaviconURI
michael@0 99 * The URI representing the favicon we are looking for.
michael@0 100 * @param aCallback
michael@0 101 * The callback where results or errors will be dispatch to. In the
michael@0 102 * returned result, the favicon binary data will be at index 0, and the
michael@0 103 * mime type will be at index 1.
michael@0 104 */
michael@0 105 nsresult GetFaviconDataAsync(nsIURI* aFaviconURI,
michael@0 106 mozIStorageStatementCallback* aCallback);
michael@0 107
michael@0 108 /**
michael@0 109 * Call to send out favicon changed notifications. Should only be called
michael@0 110 * when there is data loaded for the favicon.
michael@0 111 * @param aPageURI
michael@0 112 * The URI of the page to notify about.
michael@0 113 * @param aFaviconURI
michael@0 114 * The moz-anno:favicon URI of the icon.
michael@0 115 * @param aGUID
michael@0 116 * The unique ID associated with the page.
michael@0 117 */
michael@0 118 void SendFaviconNotifications(nsIURI* aPageURI, nsIURI* aFaviconURI,
michael@0 119 const nsACString& aGUID);
michael@0 120
michael@0 121 NS_DECL_ISUPPORTS
michael@0 122 NS_DECL_NSIFAVICONSERVICE
michael@0 123 NS_DECL_MOZIASYNCFAVICONS
michael@0 124 NS_DECL_NSITIMERCALLBACK
michael@0 125
michael@0 126 private:
michael@0 127 ~nsFaviconService();
michael@0 128
michael@0 129 nsRefPtr<mozilla::places::Database> mDB;
michael@0 130
michael@0 131 nsCOMPtr<nsITimer> mExpireUnassociatedIconsTimer;
michael@0 132
michael@0 133 static nsFaviconService* gFaviconService;
michael@0 134
michael@0 135 /**
michael@0 136 * A cached URI for the default icon. We return this a lot, and don't want to
michael@0 137 * re-parse and normalize our unchanging string many times. Important: do
michael@0 138 * not return this directly; use Clone() since callers may change the object
michael@0 139 * they get back. May be null, in which case it needs initialization.
michael@0 140 */
michael@0 141 nsCOMPtr<nsIURI> mDefaultIcon;
michael@0 142
michael@0 143 // The target dimension, in pixels, for favicons we optimize.
michael@0 144 // If we find images that are as large or larger than an uncompressed RGBA
michael@0 145 // image of this size (mOptimizedIconDimension*mOptimizedIconDimension*4),
michael@0 146 // we will try to optimize it.
michael@0 147 int32_t mOptimizedIconDimension;
michael@0 148
michael@0 149 uint32_t mFailedFaviconSerial;
michael@0 150 nsDataHashtable<nsCStringHashKey, uint32_t> mFailedFavicons;
michael@0 151
michael@0 152 // AsyncFetchAndSetIconForPage needs access to the icon cache
michael@0 153 friend class mozilla::places::AsyncFetchAndSetIconForPage;
michael@0 154 friend class mozilla::places::RemoveIconDataCacheEntry;
michael@0 155 nsTHashtable<UnassociatedIconHashKey> mUnassociatedIcons;
michael@0 156 };
michael@0 157
michael@0 158 #define FAVICON_ANNOTATION_NAME "favicon"
michael@0 159
michael@0 160 #endif // nsFaviconService_h_

mercurial