Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
* License, v. 2.0. If a copy of the MPL was not distributed with this
4
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#ifndef nsFaviconService_h_
7
#define nsFaviconService_h_
8
9
#include "nsIFaviconService.h"
10
11
#include "nsCOMPtr.h"
12
#include "nsString.h"
13
#include "nsDataHashtable.h"
14
#include "nsServiceManagerUtils.h"
15
#include "nsTHashtable.h"
16
#include "nsToolkitCompsCID.h"
17
#include "nsURIHashKey.h"
18
#include "nsINamed.h"
19
#include "nsITimer.h"
20
#include "Database.h"
21
#include "imgITools.h"
22
#include "mozilla/storage.h"
23
#include "mozilla/Attributes.h"
24
#include "mozilla/Move.h"
25
26
#include "FaviconHelpers.h"
27
28
// The target dimension in pixels for favicons we store, in reverse order.
29
// When adding/removing sizes from here, make sure to update the vector size.
30
extern const uint16_t gFaviconSizes[7];
31
32
// forward class definitions
33
class mozIStorageStatementCallback;
34
35
class UnassociatedIconHashKey : public nsURIHashKey {
36
public:
37
explicit UnassociatedIconHashKey(const nsIURI* aURI) : nsURIHashKey(aURI) {}
38
UnassociatedIconHashKey(UnassociatedIconHashKey&& aOther)
39
: nsURIHashKey(std::move(aOther)),
40
iconData(std::move(aOther.iconData)),
41
created(std::move(aOther.created)) {
42
MOZ_ASSERT_UNREACHABLE("Do not call me!");
43
}
44
mozilla::places::IconData iconData;
45
PRTime created;
46
};
47
48
class nsFaviconService final : public nsIFaviconService,
49
public nsITimerCallback,
50
public nsINamed {
51
public:
52
nsFaviconService();
53
54
/**
55
* Obtains the service's object.
56
*/
57
static already_AddRefed<nsFaviconService> GetSingleton();
58
59
/**
60
* Initializes the service's object. This should only be called once.
61
*/
62
nsresult Init();
63
64
/**
65
* Returns a cached pointer to the favicon service for consumers in the
66
* places directory.
67
*/
68
static nsFaviconService* GetFaviconService() {
69
if (!gFaviconService) {
70
nsCOMPtr<nsIFaviconService> serv =
71
do_GetService(NS_FAVICONSERVICE_CONTRACTID);
72
NS_ENSURE_TRUE(serv, nullptr);
73
NS_ASSERTION(gFaviconService, "Should have static instance pointer now");
74
}
75
return gFaviconService;
76
}
77
78
/**
79
* Fetch and migrate favicons from an unsupported payload to a supported one.
80
*/
81
static void ConvertUnsupportedPayloads(mozIStorageConnection* aDBConn);
82
83
// addition to API for strings to prevent excessive parsing of URIs
84
nsresult GetFaviconLinkForIconString(const nsCString& aIcon,
85
nsIURI** aOutput);
86
87
nsresult OptimizeIconSizes(mozilla::places::IconData& aIcon);
88
89
/**
90
* Obtains the favicon data asynchronously.
91
*
92
* @param aFaviconSpec
93
* The spec of the URI representing the favicon we are looking for.
94
* @param aCallback
95
* The callback where results or errors will be dispatch to. In the
96
* returned result, the favicon binary data will be at index 0, and the
97
* mime type will be at index 1.
98
*/
99
nsresult GetFaviconDataAsync(const nsCString& aFaviconSpec,
100
mozIStorageStatementCallback* aCallback);
101
102
/**
103
* Call to send out favicon changed notifications. Should only be called
104
* when there is data loaded for the favicon.
105
* @param aPageURI
106
* The URI of the page to notify about.
107
* @param aFaviconURI
108
* The moz-anno:favicon URI of the icon.
109
* @param aGUID
110
* The unique ID associated with the page.
111
*/
112
void SendFaviconNotifications(nsIURI* aPageURI, nsIURI* aFaviconURI,
113
const nsACString& aGUID);
114
115
static mozilla::Atomic<int64_t> sLastInsertedIconId;
116
static void StoreLastInsertedId(const nsACString& aTable,
117
const int64_t aLastInsertedId);
118
119
NS_DECL_ISUPPORTS
120
NS_DECL_NSIFAVICONSERVICE
121
NS_DECL_NSITIMERCALLBACK
122
NS_DECL_NSINAMED
123
124
private:
125
imgITools* GetImgTools() {
126
if (!mImgTools) {
127
mImgTools = do_CreateInstance("@mozilla.org/image/tools;1");
128
}
129
return mImgTools;
130
}
131
132
~nsFaviconService();
133
134
RefPtr<mozilla::places::Database> mDB;
135
136
nsCOMPtr<nsITimer> mExpireUnassociatedIconsTimer;
137
nsCOMPtr<imgITools> mImgTools;
138
139
static nsFaviconService* gFaviconService;
140
141
/**
142
* A cached URI for the default icon. We return this a lot, and don't want to
143
* re-parse and normalize our unchanging string many times. Important: do
144
* not return this directly; use Clone() since callers may change the object
145
* they get back. May be null, in which case it needs initialization.
146
*/
147
nsCOMPtr<nsIURI> mDefaultIcon;
148
149
// This class needs access to the icons cache.
150
friend class mozilla::places::AsyncReplaceFaviconData;
151
nsTHashtable<UnassociatedIconHashKey> mUnassociatedIcons;
152
153
uint16_t mDefaultIconURIPreferredSize;
154
};
155
156
#define FAVICON_ANNOTATION_NAME "favicon"
157
158
#endif // nsFaviconService_h_