mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-25 17:25:08 +02:00
Record visits as soon as a page produces useful metadata such as a title or favicon so pages that never finish loading still become autocomplete candidates. Store favicons in the history schema from the start instead of introducing an upgrade path inside this series, and cover persisted metadata behavior in TestHistoryStore.
100 lines
3.2 KiB
C++
100 lines
3.2 KiB
C++
/*
|
|
* Copyright (c) 2026-present, the Ladybird developers.
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/HashMap.h>
|
|
#include <AK/NonnullOwnPtr.h>
|
|
#include <AK/Optional.h>
|
|
#include <AK/String.h>
|
|
#include <AK/Time.h>
|
|
#include <LibDatabase/Forward.h>
|
|
#include <LibURL/Forward.h>
|
|
#include <LibWebView/Export.h>
|
|
|
|
namespace WebView {
|
|
|
|
struct WEBVIEW_API HistoryEntry {
|
|
String url;
|
|
Optional<String> title;
|
|
Optional<String> favicon_base64_png;
|
|
u64 visit_count { 0 };
|
|
UnixDateTime last_visited_time;
|
|
};
|
|
|
|
class WEBVIEW_API HistoryStore {
|
|
AK_MAKE_NONCOPYABLE(HistoryStore);
|
|
AK_MAKE_NONMOVABLE(HistoryStore);
|
|
|
|
public:
|
|
static ErrorOr<NonnullOwnPtr<HistoryStore>> create(Database::Database&);
|
|
static NonnullOwnPtr<HistoryStore> create();
|
|
static NonnullOwnPtr<HistoryStore> create_disabled();
|
|
|
|
~HistoryStore();
|
|
|
|
void record_visit(URL::URL const&, Optional<String> title = {}, UnixDateTime visited_at = UnixDateTime::now());
|
|
void update_title(URL::URL const&, String const& title);
|
|
void update_favicon(URL::URL const&, String const& favicon_base64_png);
|
|
|
|
Optional<HistoryEntry> entry_for_url(URL::URL const&);
|
|
Vector<String> autocomplete_suggestions(StringView query, size_t limit = 8);
|
|
|
|
void clear();
|
|
void remove_entries_accessed_since(UnixDateTime since);
|
|
|
|
private:
|
|
struct Statements {
|
|
Database::StatementID upsert_entry { 0 };
|
|
Database::StatementID update_title { 0 };
|
|
Database::StatementID update_favicon { 0 };
|
|
Database::StatementID get_entry { 0 };
|
|
Database::StatementID search_entries { 0 };
|
|
Database::StatementID clear_entries { 0 };
|
|
Database::StatementID delete_entries_accessed_since { 0 };
|
|
};
|
|
|
|
class TransientStorage {
|
|
public:
|
|
void record_visit(String url, Optional<String> title, UnixDateTime visited_at);
|
|
void update_title(String const& url, String title);
|
|
void update_favicon(String const& url, String favicon_base64_png);
|
|
|
|
Optional<HistoryEntry> entry_for_url(String const& url);
|
|
Vector<String> autocomplete_suggestions(StringView title_query, StringView url_query, size_t limit);
|
|
|
|
void clear();
|
|
void remove_entries_accessed_since(UnixDateTime since);
|
|
|
|
private:
|
|
HashMap<String, HistoryEntry> m_entries;
|
|
};
|
|
|
|
struct PersistedStorage {
|
|
void record_visit(String const& url, Optional<String> const& title, UnixDateTime visited_at);
|
|
void update_title(String const& url, String const& title);
|
|
void update_favicon(String const& url, String const& favicon_base64_png);
|
|
|
|
Optional<HistoryEntry> entry_for_url(String const& url);
|
|
Vector<String> autocomplete_suggestions(StringView title_query, StringView url_query, size_t limit);
|
|
|
|
void clear();
|
|
void remove_entries_accessed_since(UnixDateTime since);
|
|
|
|
Database::Database& database;
|
|
Statements statements;
|
|
};
|
|
|
|
explicit HistoryStore(Optional<PersistedStorage>, bool is_disabled = false);
|
|
static Optional<String> normalize_url(URL::URL const&);
|
|
|
|
Optional<PersistedStorage> m_persisted_storage;
|
|
TransientStorage m_transient_storage;
|
|
bool m_is_disabled { false };
|
|
};
|
|
|
|
}
|