1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Places.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Google Inc.
19 : * Portions created by the Initial Developer are Copyright (C) 2005
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Brett Wilson <brettw@gmail.com> (original author)
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef nsFaviconService_h_
40 : #define nsFaviconService_h_
41 :
42 : #include "nsIFaviconService.h"
43 : #include "mozIAsyncFavicons.h"
44 :
45 : #include "nsCOMPtr.h"
46 : #include "nsString.h"
47 : #include "nsDataHashtable.h"
48 : #include "nsServiceManagerUtils.h"
49 : #include "nsTHashtable.h"
50 : #include "nsToolkitCompsCID.h"
51 : #include "nsURIHashKey.h"
52 : #include "nsITimer.h"
53 : #include "Database.h"
54 : #include "mozilla/storage.h"
55 :
56 : #include "AsyncFaviconHelpers.h"
57 :
58 : // Favicons bigger than this size should not be saved to the db to avoid
59 : // bloating it with large image blobs.
60 : // This still allows us to accept a favicon even if we cannot optimize it.
61 : #define MAX_FAVICON_SIZE 10240
62 :
63 : // Most icons will be smaller than this rough estimate of the size of an
64 : // uncompressed 16x16 RGBA image of the same dimensions.
65 : #define MAX_ICON_FILESIZE(s) ((PRUint32) s*s*4)
66 :
67 : // forward class definitions
68 : class mozIStorageStatementCallback;
69 :
70 : class UnassociatedIconHashKey : public nsURIHashKey
71 12 : {
72 : public:
73 12 : UnassociatedIconHashKey(const nsIURI* aURI)
74 12 : : nsURIHashKey(aURI)
75 : {
76 12 : }
77 : UnassociatedIconHashKey(const UnassociatedIconHashKey& aOther)
78 : : nsURIHashKey(aOther)
79 : {
80 : NS_NOTREACHED("Do not call me!");
81 : }
82 : mozilla::places::IconData iconData;
83 : PRTime created;
84 : };
85 :
86 : class nsFaviconService : public nsIFaviconService
87 : , public mozIAsyncFavicons
88 : , public nsITimerCallback
89 : {
90 : public:
91 : nsFaviconService();
92 :
93 : /**
94 : * Obtains the service's object.
95 : */
96 : static nsFaviconService* GetSingleton();
97 :
98 : /**
99 : * Initializes the service's object. This should only be called once.
100 : */
101 : nsresult Init();
102 :
103 : static nsFaviconService* GetFaviconServiceIfAvailable() {
104 : return gFaviconService;
105 : }
106 :
107 : /**
108 : * Returns a cached pointer to the favicon service for consumers in the
109 : * places directory.
110 : */
111 63 : static nsFaviconService* GetFaviconService()
112 : {
113 63 : if (!gFaviconService) {
114 : nsCOMPtr<nsIFaviconService> serv =
115 0 : do_GetService(NS_FAVICONSERVICE_CONTRACTID);
116 0 : NS_ENSURE_TRUE(serv, nsnull);
117 0 : NS_ASSERTION(gFaviconService, "Should have static instance pointer now");
118 : }
119 63 : return gFaviconService;
120 : }
121 :
122 : // addition to API for strings to prevent excessive parsing of URIs
123 : nsresult GetFaviconLinkForIconString(const nsCString& aIcon, nsIURI** aOutput);
124 : void GetFaviconSpecForIconString(const nsCString& aIcon, nsACString& aOutput);
125 :
126 : nsresult OptimizeFaviconImage(const PRUint8* aData, PRUint32 aDataLen,
127 : const nsACString& aMimeType,
128 : nsACString& aNewData, nsACString& aNewMimeType);
129 30 : PRInt32 GetOptimizedIconDimension() { return mOptimizedIconDimension; }
130 :
131 : /**
132 : * Obtains the favicon data asynchronously.
133 : *
134 : * @param aFaviconURI
135 : * The URI representing the favicon we are looking for.
136 : * @param aCallback
137 : * The callback where results or errors will be dispatch to. In the
138 : * returned result, the favicon binary data will be at index 0, and the
139 : * mime type will be at index 1.
140 : */
141 : nsresult GetFaviconDataAsync(nsIURI* aFaviconURI,
142 : mozIStorageStatementCallback* aCallback);
143 :
144 : /**
145 : * Call to send out favicon changed notifications. Should only be called
146 : * when there is data loaded for the favicon.
147 : * @param aPageURI
148 : * The URI of the page to notify about.
149 : * @param aFaviconURI
150 : * The moz-anno:favicon URI of the icon.
151 : * @param aGUID
152 : * The unique ID associated with the page.
153 : */
154 : void SendFaviconNotifications(nsIURI* aPageURI, nsIURI* aFaviconURI,
155 : const nsACString& aGUID);
156 :
157 : NS_DECL_ISUPPORTS
158 : NS_DECL_NSIFAVICONSERVICE
159 : NS_DECL_MOZIASYNCFAVICONS
160 : NS_DECL_NSITIMERCALLBACK
161 :
162 : private:
163 : ~nsFaviconService();
164 :
165 : nsRefPtr<mozilla::places::Database> mDB;
166 :
167 : nsCOMPtr<nsITimer> mExpireUnassociatedIconsTimer;
168 :
169 : static nsFaviconService* gFaviconService;
170 :
171 : /**
172 : * A cached URI for the default icon. We return this a lot, and don't want to
173 : * re-parse and normalize our unchanging string many times. Important: do
174 : * not return this directly; use Clone() since callers may change the object
175 : * they get back. May be null, in which case it needs initialization.
176 : */
177 : nsCOMPtr<nsIURI> mDefaultIcon;
178 :
179 : // Set to true during favicons expiration, addition of new favicons won't be
180 : // allowed till expiration has finished since those should then be expired.
181 : bool mFaviconsExpirationRunning;
182 :
183 : // The target dimension, in pixels, for favicons we optimize.
184 : // If we find images that are as large or larger than an uncompressed RGBA
185 : // image of this size (mOptimizedIconDimension*mOptimizedIconDimension*4),
186 : // we will try to optimize it.
187 : PRInt32 mOptimizedIconDimension;
188 :
189 : PRUint32 mFailedFaviconSerial;
190 : nsDataHashtable<nsCStringHashKey, PRUint32> mFailedFavicons;
191 :
192 : // AsyncFetchAndSetIconForPage needs access to the icon cache
193 : friend class mozilla::places::AsyncFetchAndSetIconForPage;
194 : friend class mozilla::places::RemoveIconDataCacheEntry;
195 : nsTHashtable<UnassociatedIconHashKey> mUnassociatedIcons;
196 :
197 : // Caches the content of the default favicon if it's not already cached and
198 : // copies it into byteStr.
199 : nsresult GetDefaultFaviconData(nsCString& byteStr);
200 :
201 : // A string of bytes caching the default favicon's content. Empty if not yet
202 : // cached. Rather than accessing this directly, use GetDefaultFaviconData.
203 : nsCString mDefaultFaviconData;
204 : };
205 :
206 : #define FAVICON_ANNOTATION_NAME "favicon"
207 :
208 : #endif // nsFaviconService_h_
|