2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef ICONDATABASE_H
27 #define ICONDATABASE_H
32 #include "PlatformString.h"
33 #include "SQLDatabase.h"
34 #include "StringHash.h"
37 #include <wtf/HashMap.h>
38 #include <wtf/HashSet.h>
42 template<> struct IntHash<WebCore::IntSize> {
43 static unsigned hash(const WebCore::IntSize& key) { return intHash((static_cast<uint64_t>(key.width()) << 32 | key.height())); }
44 static bool equal(const WebCore::IntSize& a, const WebCore::IntSize& b) { return a == b; }
46 template<> struct DefaultHash<WebCore::IntSize> { typedef IntHash<WebCore::IntSize> Hash; };
59 //TODO - SiteIcon is never to be used outside of IconDatabase, so make it an internal and remove the friendness
60 friend class SiteIcon;
62 static IconDatabase* sharedIconDatabase();
64 bool open(const String& path);
65 bool isOpen() { return m_mainDB.isOpen() && m_privateBrowsingDB.isOpen(); }
68 void removeAllIcons();
72 Image* iconForPageURL(const String&, const IntSize&, bool cache = true);
73 Image* iconForIconURL(const String&, const IntSize&, bool cache = true);
74 String iconURLForPageURL(const String&);
75 Image* defaultIcon(const IntSize&);
77 void retainIconForPageURL(const String&);
78 void releaseIconForPageURL(const String&);
80 void setPrivateBrowsingEnabled(bool flag);
81 bool getPrivateBrowsingEnabled() { return m_privateBrowsingEnabled; }
83 bool hasEntryForIconURL(const String&);
85 bool isIconExpiredForIconURL(const String&);
87 void setIconDataForIconURL(const void* data, int size, const String&);
88 void setHaveNoIconForIconURL(const String&);
90 // Returns true if the set actually took place, false if the mapping already existed
91 bool setIconURLForPageURL(const String& iconURL, const String& pageURL);
93 static const String& defaultDatabaseFilename();
95 static const int currentDatabaseVersion;
96 static const int iconExpirationTime;
97 static const int missingIconExpirationTime;
98 static const int updateTimerDelay;
103 // This tries to get the iconID for the IconURL and, if it doesn't exist and createIfNecessary is true,
104 // it will create the entry and return the new iconID
105 int64_t establishIconIDForIconURL(SQLDatabase&, const String&, bool createIfNecessary = true);
107 // This method returns the SiteIcon for the given IconURL and, if it doesn't exist it creates it first
108 IconDataCache* getOrCreateIconDataCache(const String& iconURL);
110 // Remove traces of the given pageURL
111 void forgetPageURL(const String& pageURL);
113 // Remove the current database entry for this IconURL
114 void forgetIconForIconURLFromDatabase(const String&);
116 void setIconURLForPageURLInDatabase(const String&, const String&);
118 // Called by the startup timer, this method removes all icons that are unretained
119 // after initial retains are complete, and pageURLs that are dangling
120 void pruneUnretainedIconsOnStartup(Timer<IconDatabase>*);
122 // Called by the prune timer, this method periodically removes all the icons in the pending-deletion
124 void updateDatabase(Timer<IconDatabase>*);
126 // This is called by updateDatabase and when private browsing shifts, and when the DB is closed down
129 // Determine if an IconURL is still retained by anyone
130 bool isIconURLRetained(const String&);
132 // Do a quick check to make sure the database tables are in place and the db version is current
133 bool isValidDatabase(SQLDatabase&);
135 // Create the tables and triggers for the given database.
136 void createDatabaseTables(SQLDatabase&);
138 // Returns the image data for the given IconURL, checking both databases if necessary
139 void imageDataForIconURL(const String& iconURL, Vector<unsigned char>&);
141 // Retains an iconURL, bringing it back from the brink if it was pending deletion
142 void retainIconURL(const String& iconURL);
144 // Releases an iconURL, putting it on the pending delete queue if it's totally released
145 void releaseIconURL(const String& iconURL);
147 // Query - Checks for at least 1 entry in the PageURL table
148 bool pageURLTableIsEmptyQuery(SQLDatabase&);
150 // Query - Returns the time stamp for an Icon entry
151 int timeStampForIconURLQuery(SQLDatabase&, const String& iconURL);
152 SQLStatement *m_timeStampForIconURLStatement;
154 // Query - Returns the IconURL for a PageURL
155 String iconURLForPageURLQuery(SQLDatabase&, const String& pageURL);
156 SQLStatement *m_iconURLForPageURLStatement;
158 // Query - Checks for the existence of the given IconURL in the Icon table
159 bool hasIconForIconURLQuery(SQLDatabase& db, const String& iconURL);
160 SQLStatement *m_hasIconForIconURLStatement;
162 // Query - Deletes a PageURL from the PageURL table
163 void forgetPageURLQuery(SQLDatabase& db, const String& pageURL);
164 SQLStatement *m_forgetPageURLStatement;
166 // Query - Sets the Icon.iconID for a PageURL in the PageURL table
167 void setIconIDForPageURLQuery(SQLDatabase& db, int64_t, const String&);
168 SQLStatement *m_setIconIDForPageURLStatement;
170 // Query - Returns the iconID for the given IconURL
171 int64_t getIconIDForIconURLQuery(SQLDatabase& db, const String& iconURL);
172 SQLStatement *m_getIconIDForIconURLStatement;
174 // Query - Creates the Icon entry for the given IconURL and returns the resulting iconID
175 int64_t addIconForIconURLQuery(SQLDatabase& db, const String& iconURL);
176 SQLStatement *m_addIconForIconURLStatement;
178 // Query - Returns the image data from the given database for the given IconURL
179 void imageDataForIconURLQuery(SQLDatabase& db, const String& iconURL, Vector<unsigned char>& result);
180 SQLStatement *m_imageDataForIconURLStatement;
182 void deleteAllPreparedStatements(bool withSync);
184 // FIXME: This method is currently implemented in WebCoreIconDatabaseBridge so we can be in ObjC++ and fire off a loader in Webkit
185 // Once all of the loader logic is sufficiently moved into WebCore we need to move this implementation to IconDatabase.cpp
186 // using WebCore-style loaders
187 void loadIconFromURL(const String&);
189 static IconDatabase* m_sharedInstance;
191 SQLDatabase m_mainDB;
192 SQLDatabase m_privateBrowsingDB;
193 SQLDatabase* m_currentDB;
195 IconDataCache* m_defaultIconDataCache;
197 bool m_privateBrowsingEnabled;
199 Timer<IconDatabase> m_startupTimer;
200 Timer<IconDatabase> m_updateTimer;
202 bool m_initialPruningComplete;
203 SQLTransaction* m_initialPruningTransaction;
204 SQLStatement* m_preparedPageRetainInsertStatement;
206 HashMap<String, IconDataCache*> m_iconURLToIconDataCacheMap;
207 HashSet<IconDataCache*> m_iconDataCachesPendingUpdate;
209 HashMap<String, String> m_pageURLToIconURLMap;
210 HashSet<String> m_pageURLsPendingAddition;
212 // This will keep track of the retaincount for each pageURL
213 HashMap<String, int> m_pageURLToRetainCount;
214 // This will keep track of the retaincount for each iconURL (ie - the number of pageURLs retaining this icon)
215 HashMap<String, int> m_iconURLToRetainCount;
217 HashSet<String> m_pageURLsPendingDeletion;
218 HashSet<String> m_iconURLsPendingDeletion;
221 } //namespace WebCore