IconDatabase.h 9.48 KB
Newer Older
beidson's avatar
WebKit:  
beidson committed
1
/*
2
 * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3
 * Copyright (C) 2007 Justin Haygood (jhaygood@reaktix.com)
beidson's avatar
WebKit:  
beidson committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
 
27 28
#ifndef IconDatabase_h
#define IconDatabase_h
beidson's avatar
WebKit:  
beidson committed
29

30
#include "IconDatabaseBase.h"
beidson's avatar
beidson committed
31
#include "Timer.h"
32
#include <wtf/HashMap.h>
beidson's avatar
beidson committed
33
#include <wtf/HashSet.h>
beidson's avatar
beidson committed
34 35
#include <wtf/Noncopyable.h>
#include <wtf/OwnPtr.h>
36
#include <wtf/PassOwnPtr.h>
37
#include <wtf/text/StringHash.h>
38
#include <wtf/text/WTFString.h>
39

40
#if ENABLE(ICONDATABASE)
41
#include "SQLiteDatabase.h"
42
#include <wtf/Threading.h>
43
#endif // ENABLE(ICONDATABASE)
beidson's avatar
beidson committed
44

beidson's avatar
WebKit:  
beidson committed
45 46
namespace WebCore { 

beidson's avatar
beidson committed
47
class DocumentLoader;
beidson's avatar
beidson committed
48
class Image;
beidson's avatar
beidson committed
49
class IntSize;
beidson's avatar
beidson committed
50 51 52 53 54 55
class IconDatabaseClient;
class IconRecord;
class IconSnapshot;
class KURL;
class PageURLRecord;
class PageURLSnapshot;
hyatt's avatar
hyatt committed
56
class SharedBuffer;
57

58
#if ENABLE(ICONDATABASE)
beidson's avatar
beidson committed
59
class SQLTransaction;
60
#endif
beidson's avatar
beidson committed
61

62 63 64
#if !ENABLE(ICONDATABASE)
// For builds with IconDatabase disabled, they'll just use a default derivation of IconDatabaseBase. Which does nothing.
class IconDatabase : public IconDatabaseBase {
65
public:
66
    static void delayDatabaseCleanup() { }
67
    static String defaultDatabaseFilename() { return "WebpageIcons.db"; }
beidson's avatar
beidson committed
68
};
69
#else 
beidson's avatar
beidson committed
70

71 72 73
class IconDatabase : public IconDatabaseBase {
    WTF_MAKE_FAST_ALLOCATED;
    
beidson's avatar
beidson committed
74
// *** Main Thread Only ***
beidson's avatar
WebKit:  
beidson committed
75
public:
76
    static PassOwnPtr<IconDatabase> create() { return adoptPtr(new IconDatabase); }
77 78
    ~IconDatabase();

79
    virtual void setClient(IconDatabaseClient*);
beidson's avatar
beidson committed
80

81
    virtual bool open(const String& directory, const String& filename);
82
    virtual void close();
beidson's avatar
beidson committed
83
            
84
    virtual void removeAllIcons();
beidson's avatar
beidson committed
85 86

    void readIconForPageURLFromDisk(const String&);
beidson's avatar
beidson committed
87

88
    virtual Image* defaultIcon(const IntSize&);
beidson's avatar
beidson committed
89

90 91
    virtual void retainIconForPageURL(const String&);
    virtual void releaseIconForPageURL(const String&);
92
    virtual void setIconDataForIconURL(PassRefPtr<SharedBuffer> data, const String&);
93 94
    virtual void setIconURLForPageURL(const String& iconURL, const String& pageURL);

95 96 97 98
    virtual Image* synchronousIconForPageURL(const String&, const IntSize&);
    virtual String synchronousIconURLForPageURL(const String&);
    virtual bool synchronousIconDataKnownForIconURL(const String&);
    virtual IconLoadDecision synchronousLoadDecisionForIconURL(const String&, DocumentLoader*);    
beidson's avatar
beidson committed
99
    
100 101
    virtual void setEnabled(bool);
    virtual bool isEnabled() const;
beidson's avatar
beidson committed
102
    
103
    virtual void setPrivateBrowsingEnabled(bool flag);
beidson's avatar
beidson committed
104 105 106 107
    bool isPrivateBrowsingEnabled() const;
    
    static void delayDatabaseCleanup();
    static void allowDatabaseCleanup();
beidson's avatar
beidson committed
108 109
    static void checkIntegrityBeforeOpening();
        
beidson's avatar
beidson committed
110
    // Support for WebCoreStatistics in WebKit
111 112 113 114
    virtual size_t pageURLMappingCount();
    virtual size_t retainedPageURLCount();
    virtual size_t iconRecordCount();
    virtual size_t iconRecordCountWithData();
beidson's avatar
beidson committed
115

beidson's avatar
WebKit:  
beidson committed
116 117
private:
    IconDatabase();
118
    friend IconDatabaseBase& iconDatabase();
beidson's avatar
beidson committed
119

darin@apple.com's avatar
darin@apple.com committed
120 121
    static void notifyPendingLoadDecisionsOnMainThread(void*);
    void notifyPendingLoadDecisions();
beidson's avatar
beidson committed
122

beidson's avatar
beidson committed
123 124 125
    void wakeSyncThread();
    void scheduleOrDeferSyncTimer();
    void syncTimerFired(Timer<IconDatabase>*);
beidson's avatar
beidson committed
126
    
127
    Timer<IconDatabase> m_syncTimer;
128
    ThreadIdentifier m_syncThread;
beidson's avatar
beidson committed
129
    bool m_syncThreadRunning;
beidson's avatar
beidson committed
130
    
beidson's avatar
beidson committed
131
    HashSet<RefPtr<DocumentLoader> > m_loadersPendingDecision;
beidson's avatar
beidson committed
132

darin@apple.com's avatar
darin@apple.com committed
133
    RefPtr<IconRecord> m_defaultIconRecord;
beidson's avatar
beidson committed
134 135 136

// *** Any Thread ***
public:
137 138
    virtual bool isOpen() const;
    virtual String databasePath() const;
beidson's avatar
beidson committed
139 140
    static String defaultDatabaseFilename();

141
private:
darin@apple.com's avatar
darin@apple.com committed
142
    PassRefPtr<IconRecord> getOrCreateIconRecord(const String& iconURL);
beidson's avatar
beidson committed
143
    PageURLRecord* getOrCreatePageURLRecord(const String& pageURL);
beidson's avatar
beidson committed
144
    
beidson's avatar
beidson committed
145 146
    bool m_isEnabled;
    bool m_privateBrowsingEnabled;
147

beidson's avatar
beidson committed
148 149
    mutable Mutex m_syncLock;
    ThreadCondition m_syncCondition;
beidson's avatar
beidson committed
150
    String m_databaseDirectory;
beidson's avatar
beidson committed
151 152
    // Holding m_syncLock is required when accessing m_completeDatabasePath
    String m_completeDatabasePath;
153

beidson's avatar
beidson committed
154 155 156
    bool m_threadTerminationRequested;
    bool m_removeIconsRequested;
    bool m_iconURLImportComplete;
157
    bool m_syncThreadHasWorkToDo;
158 159
    bool m_disabledSuddenTerminationForSyncThread;

beidson's avatar
beidson committed
160 161 162 163 164
    Mutex m_urlAndIconLock;
    // Holding m_urlAndIconLock is required when accessing any of the following data structures or the objects they contain
    HashMap<String, IconRecord*> m_iconURLToRecordMap;
    HashMap<String, PageURLRecord*> m_pageURLToRecordMap;
    HashSet<String> m_retainedPageURLs;
beidson's avatar
beidson committed
165

beidson's avatar
beidson committed
166 167 168 169
    Mutex m_pendingSyncLock;
    // Holding m_pendingSyncLock is required when accessing any of the following data structures
    HashMap<String, PageURLSnapshot> m_pageURLsPendingSync;
    HashMap<String, IconSnapshot> m_iconsPendingSync;
beidson's avatar
beidson committed
170
    
beidson's avatar
beidson committed
171 172 173 174 175
    Mutex m_pendingReadingLock;    
    // Holding m_pendingSyncLock is required when accessing any of the following data structures - when dealing with IconRecord*s, holding m_urlAndIconLock is also required
    HashSet<String> m_pageURLsPendingImport;
    HashSet<String> m_pageURLsInterestedInIcons;
    HashSet<IconRecord*> m_iconsPendingReading;
beidson's avatar
beidson committed
176

beidson's avatar
beidson committed
177 178 179
// *** Sync Thread Only ***
public:
    // Should be used only on the sync thread and only by the Safari 2 Icons import procedure
180 181
    virtual void importIconURLForPageURL(const String& iconURL, const String& pageURL);
    virtual void importIconDataForIconURL(PassRefPtr<SharedBuffer> data, const String& iconURL);
beidson's avatar
beidson committed
182
    
183
    virtual bool shouldStopThreadActivity() const;
beidson's avatar
beidson committed
184 185 186 187

private:    
    static void* iconDatabaseSyncThreadStart(void *);
    void* iconDatabaseSyncThread();
beidson's avatar
beidson committed
188
    
beidson's avatar
beidson committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    // The following block of methods are called exclusively by the sync thread to manage i/o to and from the database
    // Each method should periodically monitor m_threadTerminationRequested when it makes sense to return early on shutdown
    void performOpenInitialization();
    bool checkIntegrity();
    void performURLImport();
    void* syncThreadMainLoop();
    bool readFromDatabase();
    bool writeToDatabase();
    void pruneUnretainedIcons();
    void checkForDanglingPageURLs(bool pruneIfFound);
    void removeAllIconsOnThread();
    void deleteAllPreparedStatements();
    void* cleanupSyncThread();

    // Record (on disk) whether or not Safari 2-style icons were imported (once per dataabse)
    bool imported();
    void setImported(bool);
beidson's avatar
beidson committed
206
    
207 208 209
    bool wasExcludedFromBackup();
    void setWasExcludedFromBackup();

beidson's avatar
beidson committed
210
    bool m_initialPruningComplete;
beidson's avatar
beidson committed
211 212 213 214 215 216 217 218 219
        
    void setIconURLForPageURLInSQLDatabase(const String&, const String&);
    void setIconIDForPageURLInSQLDatabase(int64_t, const String&);
    void removePageURLFromSQLDatabase(const String& pageURL);
    int64_t getIconIDForIconURLFromSQLDatabase(const String& iconURL);
    int64_t addIconURLToSQLDatabase(const String&);
    PassRefPtr<SharedBuffer> getImageDataForIconURLFromSQLDatabase(const String& iconURL);
    void removeIconFromSQLDatabase(const String& iconURL);
    void writeIconSnapshotToSQLDatabase(const IconSnapshot&);    
beidson's avatar
beidson committed
220
    
221 222 223 224
    // Methods to dispatch client callbacks on the main thread
    void dispatchDidImportIconURLForPageURLOnMainThread(const String&);
    void dispatchDidImportIconDataForPageURLOnMainThread(const String&);
    void dispatchDidRemoveAllIconsOnMainThread();
225
    void dispatchDidFinishURLImportOnMainThread();
226
    
beidson's avatar
beidson committed
227 228
    // The client is set by the main thread before the thread starts, and from then on is only used by the sync thread
    IconDatabaseClient* m_client;
beidson's avatar
beidson committed
229
    
beidson's avatar
beidson committed
230
    SQLiteDatabase m_syncDB;
beidson's avatar
beidson committed
231
    
beidson's avatar
beidson committed
232 233 234 235
    // Track whether the "Safari 2" import is complete and/or set in the database
    bool m_imported;
    bool m_isImportedSet;
    
beidson's avatar
beidson committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249
    OwnPtr<SQLiteStatement> m_setIconIDForPageURLStatement;
    OwnPtr<SQLiteStatement> m_removePageURLStatement;
    OwnPtr<SQLiteStatement> m_getIconIDForIconURLStatement;
    OwnPtr<SQLiteStatement> m_getImageDataForIconURLStatement;
    OwnPtr<SQLiteStatement> m_addIconToIconInfoStatement;
    OwnPtr<SQLiteStatement> m_addIconToIconDataStatement;
    OwnPtr<SQLiteStatement> m_getImageDataStatement;
    OwnPtr<SQLiteStatement> m_deletePageURLsForIconURLStatement;
    OwnPtr<SQLiteStatement> m_deleteIconFromIconInfoStatement;
    OwnPtr<SQLiteStatement> m_deleteIconFromIconDataStatement;
    OwnPtr<SQLiteStatement> m_updateIconInfoStatement;
    OwnPtr<SQLiteStatement> m_updateIconDataStatement;
    OwnPtr<SQLiteStatement> m_setIconInfoStatement;
    OwnPtr<SQLiteStatement> m_setIconDataStatement;
beidson's avatar
WebKit:  
beidson committed
250 251
};

252
#endif // !ENABLE(ICONDATABASE)
ggaren's avatar
ggaren committed
253 254

} // namespace WebCore
beidson's avatar
WebKit:  
beidson committed
255

256
#endif // IconDatabase_h