#ifndef IconDatabase_h
#define IconDatabase_h
#if ENABLE(ICONDATABASE)
#include "SQLiteDatabase.h"
#endif
#include "StringHash.h"
#if ENABLE(ICONDATABASE)
#include "Threading.h"
#endif
#include "Timer.h"
#include <wtf/HashSet.h>
#include <wtf/Noncopyable.h>
#include <wtf/OwnPtr.h>
namespace WebCore {
class DocumentLoader;
class Image;
class IntSize;
class IconDatabaseClient;
class IconRecord;
class IconSnapshot;
class KURL;
class PageURLRecord;
class PageURLSnapshot;
class SharedBuffer;
#if ENABLE(ICONDATABASE)
class SQLTransaction;
#endif
enum IconLoadDecision {
IconLoadYes,
IconLoadNo,
IconLoadUnknown
};
class IconDatabase : Noncopyable {
public:
void setClient(IconDatabaseClient*);
bool open(const String& path);
void close();
void removeAllIcons();
Image* iconForPageURL(const String&, const IntSize&, bool cache = true);
void readIconForPageURLFromDisk(const String&);
String iconURLForPageURL(const String&);
Image* defaultIcon(const IntSize&);
void retainIconForPageURL(const String&);
void releaseIconForPageURL(const String&);
void setIconDataForIconURL(PassRefPtr<SharedBuffer> data, const String&);
void setIconURLForPageURL(const String& iconURL, const String& pageURL);
IconLoadDecision loadDecisionForIconURL(const String&, DocumentLoader*);
bool iconDataKnownForIconURL(const String&);
void setEnabled(bool enabled);
bool isEnabled() const;
void setPrivateBrowsingEnabled(bool flag);
bool isPrivateBrowsingEnabled() const;
static void delayDatabaseCleanup();
static void allowDatabaseCleanup();
static void checkIntegrityBeforeOpening();
size_t pageURLMappingCount();
size_t retainedPageURLCount();
size_t iconRecordCount();
size_t iconRecordCountWithData();
private:
IconDatabase();
~IconDatabase();
friend IconDatabase* iconDatabase();
#if ENABLE(ICONDATABASE)
static void notifyPendingLoadDecisionsOnMainThread(void*);
void notifyPendingLoadDecisions();
void wakeSyncThread();
void scheduleOrDeferSyncTimer();
OwnPtr<Timer<IconDatabase> > m_syncTimer;
void syncTimerFired(Timer<IconDatabase>*);
ThreadIdentifier m_syncThread;
bool m_syncThreadRunning;
HashSet<RefPtr<DocumentLoader> > m_loadersPendingDecision;
IconRecord* m_defaultIconRecord;
#endif // ENABLE(ICONDATABASE)
public:
bool isOpen() const;
String databasePath() const;
static String defaultDatabaseFilename();
#if ENABLE(ICONDATABASE)
private:
IconRecord* getOrCreateIconRecord(const String& iconURL);
PageURLRecord* getOrCreatePageURLRecord(const String& pageURL);
bool m_isEnabled;
bool m_privateBrowsingEnabled;
mutable Mutex m_syncLock;
ThreadCondition m_syncCondition;
String m_databaseDirectory;
String m_completeDatabasePath;
bool m_threadTerminationRequested;
bool m_removeIconsRequested;
bool m_iconURLImportComplete;
Mutex m_urlAndIconLock;
HashMap<String, IconRecord*> m_iconURLToRecordMap;
HashMap<String, PageURLRecord*> m_pageURLToRecordMap;
HashSet<String> m_retainedPageURLs;
Mutex m_pendingSyncLock;
HashMap<String, PageURLSnapshot> m_pageURLsPendingSync;
HashMap<String, IconSnapshot> m_iconsPendingSync;
Mutex m_pendingReadingLock;
HashSet<String> m_pageURLsPendingImport;
HashSet<String> m_pageURLsInterestedInIcons;
HashSet<IconRecord*> m_iconsPendingReading;
public:
void importIconURLForPageURL(const String& iconURL, const String& pageURL);
void importIconDataForIconURL(PassRefPtr<SharedBuffer> data, const String& iconURL);
bool shouldStopThreadActivity() const;
private:
static void* iconDatabaseSyncThreadStart(void *);
void* iconDatabaseSyncThread();
void performOpenInitialization();
bool checkIntegrity();
void performURLImport();
void* syncThreadMainLoop();
bool readFromDatabase();
bool writeToDatabase();
void pruneUnretainedIcons();
void checkForDanglingPageURLs(bool pruneIfFound);
void removeAllIconsOnThread();
void deleteAllPreparedStatements();
void* cleanupSyncThread();
bool imported();
void setImported(bool);
bool m_initialPruningComplete;
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&);
IconDatabaseClient* m_client;
SQLiteDatabase m_syncDB;
bool m_imported;
bool m_isImportedSet;
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;
#endif // ENABLE(ICONDATABASE)
};
IconDatabase* iconDatabase();
}
#endif