#pragma once
#include "IconDatabaseBase.h"
#include <wtf/text/WTFString.h>
#if ENABLE(ICONDATABASE)
#include "SQLiteDatabase.h"
#include "Timer.h"
#include <wtf/Condition.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:
WEBCORE_EXPORT IconDatabase();
~IconDatabase();
WEBCORE_EXPORT void setClient(IconDatabaseClient*) final;
WEBCORE_EXPORT bool open(const String& directory, const String& filename) final;
WEBCORE_EXPORT void close() final;
WEBCORE_EXPORT void removeAllIcons() final;
void readIconForPageURLFromDisk(const String&);
WEBCORE_EXPORT Image* defaultIcon(const IntSize&) final;
WEBCORE_EXPORT void retainIconForPageURL(const String&) final;
WEBCORE_EXPORT void releaseIconForPageURL(const String&) final;
WEBCORE_EXPORT void setIconDataForIconURL(SharedBuffer* data, const String& iconURL) final;
WEBCORE_EXPORT void setIconURLForPageURL(const String& iconURL, const String& pageURL) final;
WEBCORE_EXPORT Image* synchronousIconForPageURL(const String&, const IntSize&) final;
NativeImagePtr synchronousNativeIconForPageURL(const String& pageURLOriginal, const IntSize&) final;
WEBCORE_EXPORT String synchronousIconURLForPageURL(const String&) final;
bool synchronousIconDataKnownForIconURL(const String&) final;
WEBCORE_EXPORT IconLoadDecision synchronousLoadDecisionForIconURL(const String&, DocumentLoader*) final;
WEBCORE_EXPORT void setEnabled(bool);
WEBCORE_EXPORT bool isEnabled() const final;
WEBCORE_EXPORT void setPrivateBrowsingEnabled(bool flag) final;
bool isPrivateBrowsingEnabled() const;
WEBCORE_EXPORT static void delayDatabaseCleanup();
WEBCORE_EXPORT static void allowDatabaseCleanup();
WEBCORE_EXPORT static void checkIntegrityBeforeOpening();
WEBCORE_EXPORT size_t pageURLMappingCount() final;
WEBCORE_EXPORT size_t retainedPageURLCount() final;
WEBCORE_EXPORT size_t iconRecordCount() final;
WEBCORE_EXPORT size_t iconRecordCountWithData() final;
private:
friend IconDatabaseBase& iconDatabase();
void notifyPendingLoadDecisions();
void wakeSyncThread();
void scheduleOrDeferSyncTimer();
void syncTimerFired();
Timer m_syncTimer;
RefPtr<Thread> m_syncThread;
bool m_syncThreadRunning;
HashSet<RefPtr<DocumentLoader>> m_loadersPendingDecision;
RefPtr<IconRecord> m_defaultIconRecord;
bool m_scheduleOrDeferSyncTimerRequested;
std::unique_ptr<SuddenTerminationDisabler> m_disableSuddenTerminationWhileSyncTimerScheduled;
public:
WEBCORE_EXPORT bool isOpen() const final;
WEBCORE_EXPORT String databasePath() const final;
WEBCORE_EXPORT static String defaultDatabaseFilename();
private:
Ref<IconRecord> getOrCreateIconRecord(const String& iconURL);
PageURLRecord* getOrCreatePageURLRecord(const String& pageURL);
bool m_isEnabled;
bool m_privateBrowsingEnabled;
mutable Lock m_syncLock;
Condition 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;
Lock m_urlAndIconLock;
HashMap<String, IconRecord*> m_iconURLToRecordMap;
HashMap<String, PageURLRecord*> m_pageURLToRecordMap;
HashSet<String> m_retainedPageURLs;
Lock m_pendingSyncLock;
HashMap<String, PageURLSnapshot> m_pageURLsPendingSync;
HashMap<String, IconSnapshot> m_iconsPendingSync;
Lock m_pendingReadingLock;
HashSet<String> m_pageURLsPendingImport;
HashSet<String> m_pageURLsInterestedInIcons;
HashSet<IconRecord*> m_iconsPendingReading;
Lock m_urlsToRetainOrReleaseLock;
HashCountedSet<String> m_urlsToRetain;
HashCountedSet<String> m_urlsToRelease;
bool m_retainOrReleaseIconRequested;
public:
WEBCORE_EXPORT bool shouldStopThreadActivity() const final;
private:
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&);
RefPtr<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;
std::unique_ptr<SQLiteStatement> m_setIconIDForPageURLStatement;
std::unique_ptr<SQLiteStatement> m_removePageURLStatement;
std::unique_ptr<SQLiteStatement> m_getIconIDForIconURLStatement;
std::unique_ptr<SQLiteStatement> m_getImageDataForIconURLStatement;
std::unique_ptr<SQLiteStatement> m_addIconToIconInfoStatement;
std::unique_ptr<SQLiteStatement> m_addIconToIconDataStatement;
std::unique_ptr<SQLiteStatement> m_getImageDataStatement;
std::unique_ptr<SQLiteStatement> m_deletePageURLsForIconURLStatement;
std::unique_ptr<SQLiteStatement> m_deleteIconFromIconInfoStatement;
std::unique_ptr<SQLiteStatement> m_deleteIconFromIconDataStatement;
std::unique_ptr<SQLiteStatement> m_updateIconInfoStatement;
std::unique_ptr<SQLiteStatement> m_updateIconDataStatement;
std::unique_ptr<SQLiteStatement> m_setIconInfoStatement;
std::unique_ptr<SQLiteStatement> m_setIconDataStatement;
};
#endif // !ENABLE(ICONDATABASE)
}