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