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