Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/toolkit/components/places/FaviconHelpers.h
Line
Count
Source (jump to first uncovered line)
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
0
#define ICON_STATUS_CHANGED 1 << 0
27
0
#define ICON_STATUS_SAVED 1 << 1
28
0
#define ICON_STATUS_ASSOCIATED 1 << 2
29
0
#define ICON_STATUS_CACHED 1 << 3
30
31
#define TO_CHARBUFFER(_buffer) \
32
0
  reinterpret_cast<char*>(const_cast<uint8_t*>(_buffer))
33
#define TO_INTBUFFER(_string) \
34
0
  reinterpret_cast<uint8_t*>(const_cast<char*>(_string.get()))
35
36
0
#define PNG_MIME_TYPE "image/png"
37
0
#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
0
#define MAX_FAVICON_EXPIRATION ((PRTime)7 * 24 * 60 * 60 * PR_USEC_PER_SEC)
46
47
// Whether there are unsupported payloads to convert yet.
48
0
#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
struct IconPayload
66
{
67
  IconPayload()
68
  : id(0)
69
  , width(0)
70
0
  {
71
0
    data.SetIsVoid(true);
72
0
    mimeType.SetIsVoid(true);
73
0
  }
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
struct IconData
85
{
86
  IconData()
87
  : expiration(0)
88
  , fetchMode(FETCH_NEVER)
89
  , status(ICON_STATUS_UNKNOWN)
90
  , rootIcon(0)
91
0
  {
92
0
  }
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
struct PageData
107
{
108
  PageData()
109
  : id(0)
110
  , placeId(0)
111
  , canAddToHistory(true)
112
0
  {
113
0
    guid.SetIsVoid(true);
114
0
  }
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
  FrameData(uint16_t aIndex, uint16_t aWidth)
131
  : index(aIndex)
132
  , width(aWidth)
133
0
  {
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
                              uint64_t aRequestContextID);
179
180
private:
181
  nsresult FetchFromNetwork();
182
0
  virtual ~AsyncFetchAndSetIconForPage() {}
183
184
  nsMainThreadPtrHandle<nsIFaviconDataCallback> mCallback;
185
  IconData mIcon;
186
  PageData mPage;
187
  const bool mFaviconLoadPrivate;
188
  nsMainThreadPtrHandle<nsIPrincipal> mLoadingPrincipal;
189
  bool mCanceled;
190
  nsCOMPtr<nsIRequest> mRequest;
191
  uint64_t mRequestContextID;
192
};
193
194
/**
195
 * Associates the icon to the required page, finally dispatches an event to the
196
 * main thread to notify the change to observers.
197
 */
198
class AsyncAssociateIconToPage final : public Runnable
199
{
200
public:
201
  NS_DECL_NSIRUNNABLE
202
203
  /**
204
   * Constructor.
205
   *
206
   * @param aIcon
207
   *        Icon to be associated.
208
   * @param aPage
209
   *        Page to which associate the icon.
210
   * @param aCallback
211
   *        Function to be called when the associate process finishes.
212
   */
213
  AsyncAssociateIconToPage(const IconData& aIcon,
214
                           const PageData& aPage,
215
                           const nsMainThreadPtrHandle<nsIFaviconDataCallback>& aCallback);
216
217
private:
218
  nsMainThreadPtrHandle<nsIFaviconDataCallback> mCallback;
219
  IconData mIcon;
220
  PageData mPage;
221
};
222
223
/**
224
 * Asynchronously tries to get the URL of a page's favicon, then notifies the
225
 * given observer.
226
 */
227
class AsyncGetFaviconURLForPage final : public Runnable
228
{
229
public:
230
  NS_DECL_NSIRUNNABLE
231
232
  /**
233
   * Constructor.
234
   *
235
   * @param aPageSpec
236
   *        URL of the page whose favicon's URL we're fetching
237
   * @param aPageHost
238
   *        Host of the page whose favicon's URL we're fetching
239
   * @param aCallback
240
   *        function to be called once finished
241
   * @param aPreferredWidth
242
   *        The preferred size for the icon
243
   */
244
  AsyncGetFaviconURLForPage(const nsACString& aPageSpec,
245
                            const nsACString& aPageHost,
246
                            uint16_t aPreferredWidth,
247
                            nsIFaviconDataCallback* aCallback);
248
249
private:
250
  uint16_t mPreferredWidth;
251
  nsMainThreadPtrHandle<nsIFaviconDataCallback> mCallback;
252
  nsCString mPageSpec;
253
  nsCString mPageHost;
254
};
255
256
257
/**
258
 * Asynchronously tries to get the URL and data of a page's favicon, then
259
 * notifies the given observer.
260
 */
261
class AsyncGetFaviconDataForPage final : public Runnable
262
{
263
public:
264
  NS_DECL_NSIRUNNABLE
265
266
  /**
267
   * Constructor.
268
   *
269
   * @param aPageSpec
270
   *        URL of the page whose favicon URL and data we're fetching
271
   * @param aPageHost
272
   *        Host of the page whose favicon's URL we're fetching
273
   * @param aPreferredWidth
274
   *        The preferred size of the icon.  We will try to return an icon close
275
   *        to this size.
276
   * @param aCallback
277
   *        function to be called once finished
278
   */
279
  AsyncGetFaviconDataForPage(const nsACString& aPageSpec,
280
                             const nsACString& aPageHost,
281
                             uint16_t aPreferredWidth,
282
                             nsIFaviconDataCallback* aCallback);
283
284
private:
285
  uint16_t mPreferredWidth;
286
  nsMainThreadPtrHandle<nsIFaviconDataCallback> mCallback;
287
  nsCString mPageSpec;
288
  nsCString mPageHost;
289
};
290
291
class AsyncReplaceFaviconData final : public Runnable
292
{
293
public:
294
  NS_DECL_NSIRUNNABLE
295
296
  explicit AsyncReplaceFaviconData(const IconData& aIcon);
297
298
private:
299
  nsresult RemoveIconDataCacheEntry();
300
301
  IconData mIcon;
302
};
303
304
/**
305
 * Notifies the icon change to favicon observers.
306
 */
307
class NotifyIconObservers final : public Runnable
308
{
309
public:
310
  NS_DECL_NSIRUNNABLE
311
312
  /**
313
   * Constructor.
314
   *
315
   * @param aIcon
316
   *        Icon information. Can be empty if no icon is associated to the page.
317
   * @param aPage
318
   *        Page to which the icon information applies.
319
   * @param aCallback
320
   *        Function to be notified in all cases.
321
   */
322
  NotifyIconObservers(const IconData& aIcon,
323
                      const PageData& aPage,
324
                      const nsMainThreadPtrHandle<nsIFaviconDataCallback>& aCallback);
325
326
private:
327
  nsMainThreadPtrHandle<nsIFaviconDataCallback> mCallback;
328
  IconData mIcon;
329
  PageData mPage;
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
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
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