#ifndef IconDatabase_h
#define IconDatabase_h
#include "IconDatabaseBase.h"
#include <wtf/text/WTFString.h>
#if ENABLE(ICONDATABASE)
#include "SQLiteDatabase.h"
#include "Timer.h"
#include <wtf/HashCountedSet.h>
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
#endif
namespace WebCore {
#if !ENABLE(ICONDATABASE)
class IconDatabase final : public IconDatabaseBase {
WTF_MAKE_FAST_ALLOCATED;
public:
static void delayDatabaseCleanup() { }
static void allowDatabaseCleanup() { }
static void checkIntegrityBeforeOpening() { }
static String defaultDatabaseFilename() { return ASCIILiteral("WebpageIcons.db"); }
};
#else
class IconRecord;
class IconSnapshot;
class PageURLRecord;
class PageURLSnapshot;
class SuddenTerminationDisabler;
class IconDatabase final : public IconDatabaseBase {
WTF_MAKE_FAST_ALLOCATED;
public:
IconDatabase();
~IconDatabase();
virtual void setClient(IconDatabaseClient*) override;
virtual bool open(const String& directory, const String& filename) override;
virtual void close() override;
virtual void removeAllIcons() override;
void readIconForPageURLFromDisk(const String&);
virtual Image* defaultIcon(const IntSize&) override;
virtual void retainIconForPageURL(const String&) override;
virtual void releaseIconForPageURL(const String&) override;
virtual void setIconDataForIconURL(PassRefPtr<SharedBuffer> data, const String&) override;
virtual void setIconURLForPageURL(const String& iconURL, const String& pageURL) override;
virtual Image* synchronousIconForPageURL(const String&, const IntSize&) override;
virtual PassNativeImagePtr synchronousNativeIconForPageURL(const String& pageURLOriginal, const IntSize&) override;
virtual String synchronousIconURLForPageURL(const String&) override;
virtual bool synchronousIconDataKnownForIconURL(const String&) override;
virtual IconLoadDecision synchronousLoadDecisionForIconURL(const String&, DocumentLoader*) override;
virtual void setEnabled(bool) override;
virtual bool isEnabled() const override;
virtual void setPrivateBrowsingEnabled(bool flag) override;
bool isPrivateBrowsingEnabled() const;
static void delayDatabaseCleanup();
static void allowDatabaseCleanup();
static void checkIntegrityBeforeOpening();
virtual size_t pageURLMappingCount() override;
virtual size_t retainedPageURLCount() override;
virtual size_t iconRecordCount() override;
virtual size_t iconRecordCountWithData() override;
private:
friend IconDatabaseBase& iconDatabase();
void notifyPendingLoadDecisions();
void wakeSyncThread();
void scheduleOrDeferSyncTimer();
void syncTimerFired(Timer&);
Timer m_syncTimer;
ThreadIdentifier m_syncThread;
bool m_syncThreadRunning;
HashSet<RefPtr<DocumentLoader>> m_loadersPendingDecision;
RefPtr<IconRecord> m_defaultIconRecord;
bool m_scheduleOrDeferSyncTimerRequested;
std::unique_ptr<SuddenTerminationDisabler> m_disableSuddenTerminationWhileSyncTimerScheduled;
public:
virtual bool isOpen() const;
virtual String databasePath() const;
static String defaultDatabaseFilename();
private:
PassRefPtr<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;
bool m_syncThreadHasWorkToDo;
std::unique_ptr<SuddenTerminationDisabler> m_disableSuddenTerminationWhileSyncThreadHasWorkToDo;
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;
Mutex m_urlsToRetainOrReleaseLock;
HashCountedSet<String> m_urlsToRetain;
HashCountedSet<String> m_urlsToRelease;
bool m_retainOrReleaseIconRequested;
public:
virtual 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();
void performRetainIconForPageURL(const String&, int retainCount);
void performReleaseIconForPageURL(const String&, int releaseCount);
bool wasExcludedFromBackup();
void setWasExcludedFromBackup();
bool isOpenBesidesMainThreadCallbacks() const;
void checkClosedAfterMainThreadCallback();
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&);
void performPendingRetainAndReleaseOperations();
void dispatchDidImportIconURLForPageURLOnMainThread(const String&);
void dispatchDidImportIconDataForPageURLOnMainThread(const String&);
void dispatchDidRemoveAllIconsOnMainThread();
void dispatchDidFinishURLImportOnMainThread();
std::atomic<uint32_t> m_mainThreadCallbackCount;
IconDatabaseClient* m_client;
SQLiteDatabase m_syncDB;
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)
}
#endif // IconDatabase_h