Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
7 #ifndef AsyncFaviconHelpers_h_
8 #define AsyncFaviconHelpers_h_
10 #include "nsIFaviconService.h"
11 #include "nsIChannelEventSink.h"
12 #include "nsIInterfaceRequestor.h"
13 #include "nsIStreamListener.h"
14 #include "nsThreadUtils.h"
16 #include "Database.h"
17 #include "mozilla/storage.h"
19 #define ICON_STATUS_UNKNOWN 0
20 #define ICON_STATUS_CHANGED 1 << 0
21 #define ICON_STATUS_SAVED 1 << 1
22 #define ICON_STATUS_ASSOCIATED 1 << 2
23 #define ICON_STATUS_CACHED 1 << 3
25 #define TO_CHARBUFFER(_buffer) \
26 reinterpret_cast<char*>(const_cast<uint8_t*>(_buffer))
27 #define TO_INTBUFFER(_string) \
28 reinterpret_cast<uint8_t*>(const_cast<char*>(_string.get()))
30 /**
31 * The maximum time we will keep a favicon around. We always ask the cache, if
32 * we can, but default to this value if we do not get a time back, or the time
33 * is more in the future than this.
34 * Currently set to one week from now.
35 */
36 #define MAX_FAVICON_EXPIRATION ((PRTime)7 * 24 * 60 * 60 * PR_USEC_PER_SEC)
38 namespace mozilla {
39 namespace places {
41 /**
42 * Indicates when a icon should be fetched from network.
43 */
44 enum AsyncFaviconFetchMode {
45 FETCH_NEVER = 0
46 , FETCH_IF_MISSING
47 , FETCH_ALWAYS
48 };
50 /**
51 * Data cache for a icon entry.
52 */
53 struct IconData
54 {
55 IconData()
56 : id(0)
57 , expiration(0)
58 , fetchMode(FETCH_NEVER)
59 , status(ICON_STATUS_UNKNOWN)
60 {
61 guid.SetIsVoid(true);
62 }
64 int64_t id;
65 nsCString spec;
66 nsCString data;
67 nsCString mimeType;
68 PRTime expiration;
69 enum AsyncFaviconFetchMode fetchMode;
70 uint16_t status; // This is a bitset, see ICON_STATUS_* defines above.
71 nsCString guid;
72 };
74 /**
75 * Data cache for a page entry.
76 */
77 struct PageData
78 {
79 PageData()
80 : id(0)
81 , canAddToHistory(true)
82 , iconId(0)
83 {
84 guid.SetIsVoid(true);
85 }
87 int64_t id;
88 nsCString spec;
89 nsCString bookmarkedSpec;
90 nsString revHost;
91 bool canAddToHistory; // False for disabled history and unsupported schemas.
92 int64_t iconId;
93 nsCString guid;
94 };
96 /**
97 * Base class for events declared in this file. This class's main purpose is
98 * to declare a destructor which releases mCallback on the main thread.
99 */
100 class AsyncFaviconHelperBase : public nsRunnable
101 {
102 protected:
103 AsyncFaviconHelperBase(nsCOMPtr<nsIFaviconDataCallback>& aCallback);
105 virtual ~AsyncFaviconHelperBase();
107 nsRefPtr<Database> mDB;
108 // Strong reference since we are responsible for its existence.
109 nsCOMPtr<nsIFaviconDataCallback> mCallback;
110 };
112 /**
113 * Async fetches icon from database or network, associates it with the required
114 * page and finally notifies the change.
115 */
116 class AsyncFetchAndSetIconForPage : public AsyncFaviconHelperBase
117 {
118 public:
119 NS_DECL_NSIRUNNABLE
121 /**
122 * Creates the event and dispatches it to the async thread.
123 *
124 * @param aFaviconURI
125 * URI of the icon to be fetched and associated.
126 * @param aPageURI
127 * URI of the page to which associate the icon.
128 * @param aFetchMode
129 * Specifies whether a icon should be fetched from network if not found
130 * in the database.
131 * @param aCallback
132 * Function to be called when the fetch-and-associate process finishes.
133 */
134 static nsresult start(nsIURI* aFaviconURI,
135 nsIURI* aPageURI,
136 enum AsyncFaviconFetchMode aFetchMode,
137 uint32_t aFaviconLoadType,
138 nsIFaviconDataCallback* aCallback);
140 /**
141 * Constructor.
142 *
143 * @param aIcon
144 * Icon to be fetched and associated.
145 * @param aPage
146 * Page to which associate the icon.
147 * @param aCallback
148 * Function to be called when the fetch-and-associate process finishes.
149 */
150 AsyncFetchAndSetIconForPage(IconData& aIcon,
151 PageData& aPage,
152 uint32_t aFaviconLoadType,
153 nsCOMPtr<nsIFaviconDataCallback>& aCallback);
155 virtual ~AsyncFetchAndSetIconForPage();
157 protected:
158 IconData mIcon;
159 PageData mPage;
160 const bool mFaviconLoadPrivate;
161 };
163 /**
164 * If needed will asynchronously fetch the icon from the network. It will
165 * finally dispatch an event to the async thread to associate the icon with
166 * the required page.
167 */
168 class AsyncFetchAndSetIconFromNetwork : public AsyncFaviconHelperBase
169 , public nsIStreamListener
170 , public nsIInterfaceRequestor
171 , public nsIChannelEventSink
172 {
173 public:
174 NS_DECL_NSISTREAMLISTENER
175 NS_DECL_NSIINTERFACEREQUESTOR
176 NS_DECL_NSICHANNELEVENTSINK
177 NS_DECL_NSIREQUESTOBSERVER
178 NS_DECL_NSIRUNNABLE
179 NS_DECL_ISUPPORTS_INHERITED
181 /**
182 * Constructor.
183 *
184 * @param aIcon
185 * Icon to be fetched and associated.
186 * @param aPage
187 * Page to which associate the icon.
188 * @param aCallback
189 * Function to be called when the fetch-and-associate process finishes.
190 */
191 AsyncFetchAndSetIconFromNetwork(IconData& aIcon,
192 PageData& aPage,
193 bool aFaviconLoadPrivate,
194 nsCOMPtr<nsIFaviconDataCallback>& aCallback);
196 virtual ~AsyncFetchAndSetIconFromNetwork();
198 protected:
199 IconData mIcon;
200 PageData mPage;
201 const bool mFaviconLoadPrivate;
202 };
204 /**
205 * Associates the icon to the required page, finally dispatches an event to the
206 * main thread to notify the change to observers.
207 */
208 class AsyncAssociateIconToPage : public AsyncFaviconHelperBase
209 {
210 public:
211 NS_DECL_NSIRUNNABLE
213 /**
214 * Constructor.
215 *
216 * @param aIcon
217 * Icon to be associated.
218 * @param aPage
219 * Page to which associate the icon.
220 * @param aCallback
221 * Function to be called when the associate process finishes.
222 */
223 AsyncAssociateIconToPage(IconData& aIcon,
224 PageData& aPage,
225 nsCOMPtr<nsIFaviconDataCallback>& aCallback);
227 virtual ~AsyncAssociateIconToPage();
229 protected:
230 IconData mIcon;
231 PageData mPage;
232 };
234 /**
235 * Asynchronously tries to get the URL of a page's favicon, then notifies the
236 * given observer.
237 */
238 class AsyncGetFaviconURLForPage : public AsyncFaviconHelperBase
239 {
240 public:
241 NS_DECL_NSIRUNNABLE
243 /**
244 * Creates the event and dispatches it to the I/O thread.
245 *
246 * @param aPageURI
247 * URL of the page whose favicon's URL we're fetching
248 * @param aCallback
249 * function to be called once finished
250 */
251 static nsresult start(nsIURI* aPageURI,
252 nsIFaviconDataCallback* aCallback);
254 /**
255 * Constructor.
256 *
257 * @param aPageSpec
258 * URL of the page whose favicon's URL we're fetching
259 * @param aCallback
260 * function to be called once finished
261 */
262 AsyncGetFaviconURLForPage(const nsACString& aPageSpec,
263 nsCOMPtr<nsIFaviconDataCallback>& aCallback);
265 virtual ~AsyncGetFaviconURLForPage();
267 private:
268 nsCString mPageSpec;
269 };
272 /**
273 * Asynchronously tries to get the URL and data of a page's favicon, then
274 * notifies the given observer.
275 */
276 class AsyncGetFaviconDataForPage : public AsyncFaviconHelperBase
277 {
278 public:
279 NS_DECL_NSIRUNNABLE
281 /**
282 * Creates the event and dispatches it to the I/O thread.
283 *
284 * @param aPageURI
285 * URL of the page whose favicon URL and data we're fetching
286 * @param aCallback
287 * function to be called once finished
288 */
289 static nsresult start(nsIURI* aPageURI,
290 nsIFaviconDataCallback* aCallback);
292 /**
293 * Constructor.
294 *
295 * @param aPageSpec
296 * URL of the page whose favicon URL and data we're fetching
297 * @param aCallback
298 * function to be called once finished
299 */
300 AsyncGetFaviconDataForPage(const nsACString& aPageSpec,
301 nsCOMPtr<nsIFaviconDataCallback>& aCallback);
303 virtual ~AsyncGetFaviconDataForPage();
305 private:
306 nsCString mPageSpec;
307 };
309 class AsyncReplaceFaviconData : public AsyncFaviconHelperBase
310 {
311 public:
312 NS_DECL_NSIRUNNABLE
314 static nsresult start(IconData *aIcon);
316 AsyncReplaceFaviconData(IconData &aIcon,
317 nsCOMPtr<nsIFaviconDataCallback>& aCallback);
319 virtual ~AsyncReplaceFaviconData();
321 protected:
322 IconData mIcon;
323 };
325 class RemoveIconDataCacheEntry : public AsyncFaviconHelperBase
326 {
327 public:
328 NS_DECL_NSIRUNNABLE
330 RemoveIconDataCacheEntry(IconData &aIcon,
331 nsCOMPtr<nsIFaviconDataCallback>& aCallback);
332 virtual ~RemoveIconDataCacheEntry();
334 protected:
335 IconData mIcon;
336 };
338 /**
339 * Notifies the icon change to favicon observers.
340 */
341 class NotifyIconObservers : public AsyncFaviconHelperBase
342 {
343 public:
344 NS_DECL_NSIRUNNABLE
346 /**
347 * Constructor.
348 *
349 * @param aIcon
350 * Icon information. Can be empty if no icon is associated to the page.
351 * @param aPage
352 * Page to which the icon information applies.
353 * @param aCallback
354 * Function to be notified in all cases.
355 */
356 NotifyIconObservers(IconData& aIcon,
357 PageData& aPage,
358 nsCOMPtr<nsIFaviconDataCallback>& aCallback);
359 virtual ~NotifyIconObservers();
361 protected:
362 IconData mIcon;
363 PageData mPage;
365 void SendGlobalNotifications(nsIURI* aIconURI);
366 };
368 } // namespace places
369 } // namespace mozilla
371 #endif // AsyncFaviconHelpers_h_