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