Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
3 : * This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #pragma once
8 :
9 : #include "nsIFaviconService.h"
10 : #include "nsIChannelEventSink.h"
11 : #include "nsIInterfaceRequestor.h"
12 : #include "nsIStreamListener.h"
13 : #include "mozIPlacesPendingOperation.h"
14 : #include "nsThreadUtils.h"
15 : #include "nsProxyRelease.h"
16 : #include "imgITools.h"
17 : #include "imgIContainer.h"
18 : #include "imgLoader.h"
19 :
20 : class nsIPrincipal;
21 :
22 : #include "Database.h"
23 : #include "mozilla/storage.h"
24 :
25 : #define ICON_STATUS_UNKNOWN 0
26 : #define ICON_STATUS_CHANGED 1 << 0
27 : #define ICON_STATUS_SAVED 1 << 1
28 : #define ICON_STATUS_ASSOCIATED 1 << 2
29 : #define ICON_STATUS_CACHED 1 << 3
30 :
31 : #define TO_CHARBUFFER(_buffer) \
32 : reinterpret_cast<char*>(const_cast<uint8_t*>(_buffer))
33 : #define TO_INTBUFFER(_string) \
34 : reinterpret_cast<uint8_t*>(const_cast<char*>(_string.get()))
35 :
36 : #define PNG_MIME_TYPE "image/png"
37 : #define SVG_MIME_TYPE "image/svg+xml"
38 :
39 : /**
40 : * The maximum time we will keep a favicon around. We always ask the cache, if
41 : * we can, but default to this value if we do not get a time back, or the time
42 : * is more in the future than this.
43 : * Currently set to one week from now.
44 : */
45 : #define MAX_FAVICON_EXPIRATION ((PRTime)7 * 24 * 60 * 60 * PR_USEC_PER_SEC)
46 :
47 : // Whether there are unsupported payloads to convert yet.
48 : #define PREF_CONVERT_PAYLOADS "places.favicons.convertPayloads"
49 :
50 : namespace mozilla {
51 : namespace places {
52 :
53 : /**
54 : * Indicates when a icon should be fetched from network.
55 : */
56 : enum AsyncFaviconFetchMode {
57 : FETCH_NEVER = 0
58 : , FETCH_IF_MISSING
59 : , FETCH_ALWAYS
60 : };
61 :
62 : /**
63 : * Represents one of the payloads (frames) of an icon entry.
64 : */
65 2 : struct IconPayload
66 : {
67 1 : IconPayload()
68 1 : : id(0)
69 1 : , width(0)
70 : {
71 1 : data.SetIsVoid(true);
72 1 : mimeType.SetIsVoid(true);
73 1 : }
74 :
75 : int64_t id;
76 : uint16_t width;
77 : nsCString data;
78 : nsCString mimeType;
79 : };
80 :
81 : /**
82 : * Represents an icon entry.
83 : */
84 2 : struct IconData
85 : {
86 1 : IconData()
87 1 : : expiration(0)
88 : , fetchMode(FETCH_NEVER)
89 : , status(ICON_STATUS_UNKNOWN)
90 1 : , rootIcon(0)
91 : {
92 1 : }
93 :
94 : nsCString spec;
95 : nsCString host;
96 : PRTime expiration;
97 : enum AsyncFaviconFetchMode fetchMode;
98 : uint16_t status; // This is a bitset, see ICON_STATUS_* defines above.
99 : uint8_t rootIcon;
100 : nsTArray<IconPayload> payloads;
101 : };
102 :
103 : /**
104 : * Data cache for a page entry.
105 : */
106 2 : struct PageData
107 : {
108 1 : PageData()
109 1 : : id(0)
110 : , placeId(0)
111 1 : , canAddToHistory(true)
112 : {
113 1 : guid.SetIsVoid(true);
114 1 : }
115 :
116 : int64_t id; // This is the moz_pages_w_icons id.
117 : int64_t placeId; // This is the moz_places page id.
118 : nsCString spec;
119 : nsCString host;
120 : nsCString bookmarkedSpec;
121 : bool canAddToHistory; // False for disabled history and unsupported schemas.
122 : nsCString guid;
123 : };
124 :
125 : /**
126 : * Info for a frame.
127 : */
128 : struct FrameData
129 : {
130 0 : FrameData(uint16_t aIndex, uint16_t aWidth)
131 0 : : index(aIndex)
132 0 : , width(aWidth)
133 : {
134 0 : }
135 :
136 : uint16_t index;
137 : uint16_t width;
138 : };
139 :
140 : /**
141 : * Async fetches icon from database or network, associates it with the required
142 : * page and finally notifies the change.
143 : */
144 : class AsyncFetchAndSetIconForPage final : public Runnable
145 : , public nsIStreamListener
146 : , public nsIInterfaceRequestor
147 : , public nsIChannelEventSink
148 : , public mozIPlacesPendingOperation
149 : {
150 : public:
151 : NS_DECL_NSIRUNNABLE
152 : NS_DECL_NSISTREAMLISTENER
153 : NS_DECL_NSIINTERFACEREQUESTOR
154 : NS_DECL_NSICHANNELEVENTSINK
155 : NS_DECL_NSIREQUESTOBSERVER
156 : NS_DECL_MOZIPLACESPENDINGOPERATION
157 : NS_DECL_ISUPPORTS_INHERITED
158 :
159 : /**
160 : * Constructor.
161 : *
162 : * @param aIcon
163 : * Icon to be fetched and associated.
164 : * @param aPage
165 : * Page to which associate the icon.
166 : * @param aFaviconLoadPrivate
167 : * Whether this favicon load is in private browsing.
168 : * @param aCallback
169 : * Function to be called when the fetch-and-associate process finishes.
170 : * @param aLoadingPrincipal
171 : * LoadingPrincipal of the icon to be fetched.
172 : */
173 : AsyncFetchAndSetIconForPage(IconData& aIcon,
174 : PageData& aPage,
175 : bool aFaviconLoadPrivate,
176 : nsIFaviconDataCallback* aCallback,
177 : nsIPrincipal* aLoadingPrincipal);
178 :
179 : private:
180 : nsresult FetchFromNetwork();
181 0 : virtual ~AsyncFetchAndSetIconForPage() {}
182 :
183 : nsMainThreadPtrHandle<nsIFaviconDataCallback> mCallback;
184 : IconData mIcon;
185 : PageData mPage;
186 : const bool mFaviconLoadPrivate;
187 : nsMainThreadPtrHandle<nsIPrincipal> mLoadingPrincipal;
188 : bool mCanceled;
189 : nsCOMPtr<nsIRequest> mRequest;
190 : };
191 :
192 : /**
193 : * Associates the icon to the required page, finally dispatches an event to the
194 : * main thread to notify the change to observers.
195 : */
196 0 : class AsyncAssociateIconToPage final : public Runnable
197 : {
198 : public:
199 : NS_DECL_NSIRUNNABLE
200 :
201 : /**
202 : * Constructor.
203 : *
204 : * @param aIcon
205 : * Icon to be associated.
206 : * @param aPage
207 : * Page to which associate the icon.
208 : * @param aCallback
209 : * Function to be called when the associate process finishes.
210 : */
211 : AsyncAssociateIconToPage(const IconData& aIcon,
212 : const PageData& aPage,
213 : const nsMainThreadPtrHandle<nsIFaviconDataCallback>& aCallback);
214 :
215 : private:
216 : nsMainThreadPtrHandle<nsIFaviconDataCallback> mCallback;
217 : IconData mIcon;
218 : PageData mPage;
219 : };
220 :
221 : /**
222 : * Asynchronously tries to get the URL of a page's favicon, then notifies the
223 : * given observer.
224 : */
225 0 : class AsyncGetFaviconURLForPage final : public Runnable
226 : {
227 : public:
228 : NS_DECL_NSIRUNNABLE
229 :
230 : /**
231 : * Constructor.
232 : *
233 : * @param aPageSpec
234 : * URL of the page whose favicon's URL we're fetching
235 : * @param aPageHost
236 : * Host of the page whose favicon's URL we're fetching
237 : * @param aCallback
238 : * function to be called once finished
239 : * @param aPreferredWidth
240 : * The preferred size for the icon
241 : */
242 : AsyncGetFaviconURLForPage(const nsACString& aPageSpec,
243 : const nsACString& aPageHost,
244 : uint16_t aPreferredWidth,
245 : nsIFaviconDataCallback* aCallback);
246 :
247 : private:
248 : uint16_t mPreferredWidth;
249 : nsMainThreadPtrHandle<nsIFaviconDataCallback> mCallback;
250 : nsCString mPageSpec;
251 : nsCString mPageHost;
252 : };
253 :
254 :
255 : /**
256 : * Asynchronously tries to get the URL and data of a page's favicon, then
257 : * notifies the given observer.
258 : */
259 0 : class AsyncGetFaviconDataForPage final : public Runnable
260 : {
261 : public:
262 : NS_DECL_NSIRUNNABLE
263 :
264 : /**
265 : * Constructor.
266 : *
267 : * @param aPageSpec
268 : * URL of the page whose favicon URL and data we're fetching
269 : * @param aPageHost
270 : * Host of the page whose favicon's URL we're fetching
271 : * @param aPreferredWidth
272 : * The preferred size of the icon. We will try to return an icon close
273 : * to this size.
274 : * @param aCallback
275 : * function to be called once finished
276 : */
277 : AsyncGetFaviconDataForPage(const nsACString& aPageSpec,
278 : const nsACString& aPageHost,
279 : uint16_t aPreferredWidth,
280 : nsIFaviconDataCallback* aCallback);
281 :
282 : private:
283 : uint16_t mPreferredWidth;
284 : nsMainThreadPtrHandle<nsIFaviconDataCallback> mCallback;
285 : nsCString mPageSpec;
286 : nsCString mPageHost;
287 : };
288 :
289 0 : class AsyncReplaceFaviconData final : public Runnable
290 : {
291 : public:
292 : NS_DECL_NSIRUNNABLE
293 :
294 : explicit AsyncReplaceFaviconData(const IconData& aIcon);
295 :
296 : private:
297 : nsresult RemoveIconDataCacheEntry();
298 :
299 : IconData mIcon;
300 : };
301 :
302 : /**
303 : * Notifies the icon change to favicon observers.
304 : */
305 0 : class NotifyIconObservers final : public Runnable
306 : {
307 : public:
308 : NS_DECL_NSIRUNNABLE
309 :
310 : /**
311 : * Constructor.
312 : *
313 : * @param aIcon
314 : * Icon information. Can be empty if no icon is associated to the page.
315 : * @param aPage
316 : * Page to which the icon information applies.
317 : * @param aCallback
318 : * Function to be notified in all cases.
319 : */
320 : NotifyIconObservers(const IconData& aIcon,
321 : const PageData& aPage,
322 : const nsMainThreadPtrHandle<nsIFaviconDataCallback>& aCallback);
323 :
324 : private:
325 : nsMainThreadPtrHandle<nsIFaviconDataCallback> mCallback;
326 : IconData mIcon;
327 : PageData mPage;
328 :
329 : void SendGlobalNotifications(nsIURI* aIconURI);
330 : };
331 :
332 : /**
333 : * Fetches and converts unsupported payloads. This is used during the initial
334 : * migration of icons from the old to the new store.
335 : */
336 0 : class FetchAndConvertUnsupportedPayloads final : public Runnable
337 : {
338 : public:
339 : NS_DECL_NSIRUNNABLE
340 :
341 : /**
342 : * Constructor.
343 : *
344 : * @param aDBConn
345 : * The database connection to use.
346 : */
347 : explicit FetchAndConvertUnsupportedPayloads(mozIStorageConnection* aDBConn);
348 :
349 : private:
350 : nsresult ConvertPayload(int64_t aId, const nsACString& aMimeType,
351 : nsCString& aPayload, int32_t* aWidth);
352 : nsresult StorePayload(int64_t aId, int32_t aWidth, const nsCString& aPayload);
353 :
354 : nsCOMPtr<mozIStorageConnection> mDB;
355 : };
356 :
357 : /**
358 : * Copies Favicons from one page to another one.
359 : */
360 0 : class AsyncCopyFavicons final : public Runnable
361 : {
362 : public:
363 : NS_DECL_NSIRUNNABLE
364 :
365 : /**
366 : * Constructor.
367 : *
368 : * @param aFromPage
369 : * The originating page.
370 : * @param aToPage
371 : * The destination page.
372 : * @param aFaviconLoadPrivate
373 : * Whether this favicon load is in private browsing.
374 : * @param aCallback
375 : * An optional callback to invoke when done.
376 : */
377 : AsyncCopyFavicons(PageData& aFromPage,
378 : PageData& aToPage,
379 : nsIFaviconDataCallback* aCallback);
380 :
381 : private:
382 : PageData mFromPage;
383 : PageData mToPage;
384 : nsMainThreadPtrHandle<nsIFaviconDataCallback> mCallback;
385 : };
386 :
387 : } // namespace places
388 : } // namespace mozilla
|