Persist site history to disk and integrate with settings
Add persistent site history storage and plumbing between the renderer settings UI and the native app. The app now loads/saves site_history.txt in the user data directory (max 200 entries, http/https-only, stored one URL per line) and records visited sites on navigation. Settings pages receive the history via injected JavaScript when the settings page finishes loading, and a "clear-site-history" message from the settings UI clears the on-disk history and updates the renderer. Other changes: allow settings-related process messages from content frames in the CEF client, introduce OnContentLoadFinished to trigger history injection, expose electronAPI.send/sendToHost (and reuse the native postMessage handler) in the V8 context, and remove the BigPicture in-app history UI/refresh/clear handlers (history is now managed by the native app). Also cleaned up includes and added helper utilities for JSON escaping, lowercasing, and file path handling. The initial tab restore logic was simplified to always create an initial tab (home or initial_url) and persist the session.
This commit is contained in:
@@ -4,6 +4,9 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <charconv>
|
||||
#include <cctype>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <system_error>
|
||||
|
||||
#include "browser/session_state.h"
|
||||
@@ -17,6 +20,8 @@
|
||||
namespace nebula::app {
|
||||
namespace {
|
||||
|
||||
constexpr size_t kMaxSiteHistoryEntries = 200;
|
||||
|
||||
std::wstring Utf8ToWide(const std::string& value) {
|
||||
if (value.empty()) {
|
||||
return {};
|
||||
@@ -30,6 +35,67 @@ std::wstring Utf8ToWide(const std::string& value) {
|
||||
return result;
|
||||
}
|
||||
|
||||
std::filesystem::path GetSiteHistoryPath() {
|
||||
const auto user_data = nebula::ui::GetUserDataDirectory();
|
||||
return user_data.empty() ? std::filesystem::path{} : user_data / L"site_history.txt";
|
||||
}
|
||||
|
||||
std::string ToLowerAscii(std::string value) {
|
||||
std::transform(value.begin(), value.end(), value.begin(), [](unsigned char ch) {
|
||||
return static_cast<char>(std::tolower(ch));
|
||||
});
|
||||
return value;
|
||||
}
|
||||
|
||||
bool IsSiteHistoryUrl(const std::string& url) {
|
||||
const std::string lower = ToLowerAscii(url);
|
||||
return lower.starts_with("http://") || lower.starts_with("https://");
|
||||
}
|
||||
|
||||
std::vector<std::string> LoadSiteHistory() {
|
||||
std::vector<std::string> history;
|
||||
std::ifstream input(GetSiteHistoryPath(), std::ios::binary);
|
||||
if (!input) {
|
||||
return history;
|
||||
}
|
||||
|
||||
std::string url;
|
||||
while (std::getline(input, url) && history.size() < kMaxSiteHistoryEntries) {
|
||||
if (IsSiteHistoryUrl(url)) {
|
||||
history.push_back(url);
|
||||
}
|
||||
}
|
||||
return history;
|
||||
}
|
||||
|
||||
void SaveSiteHistory(const std::vector<std::string>& history) {
|
||||
const auto path = GetSiteHistoryPath();
|
||||
if (path.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::ofstream output(path, std::ios::binary | std::ios::trunc);
|
||||
if (!output) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto& url : history) {
|
||||
output << url << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
std::string SiteHistoryJson(const std::vector<std::string>& history) {
|
||||
std::string json = "[";
|
||||
for (size_t i = 0; i < history.size(); ++i) {
|
||||
if (i > 0) {
|
||||
json += ",";
|
||||
}
|
||||
json += "\"" + nebula::browser::JsonEscape(history[i]) + "\"";
|
||||
}
|
||||
json += "]";
|
||||
return json;
|
||||
}
|
||||
|
||||
CefWindowInfo ChildWindowInfo(HWND parent, const RECT& rect) {
|
||||
CefWindowInfo info;
|
||||
info.SetAsChild(
|
||||
@@ -141,7 +207,8 @@ NebulaController::NebulaController(HINSTANCE instance, std::string initial_url,
|
||||
: instance_(instance),
|
||||
initial_url_(std::move(initial_url)),
|
||||
show_command_(show_command),
|
||||
tabs_(this) {}
|
||||
tabs_(this),
|
||||
site_history_(LoadSiteHistory()) {}
|
||||
|
||||
NebulaController::~NebulaController() = default;
|
||||
|
||||
@@ -152,15 +219,11 @@ bool NebulaController::Create() {
|
||||
|
||||
void NebulaController::OnWindowCreated() {
|
||||
if (initial_url_.empty()) {
|
||||
const auto session = nebula::browser::LoadSessionState();
|
||||
if (!session.tabs.empty()) {
|
||||
tabs_.RestoreTabs(session.tabs, session.active_tab_index);
|
||||
} else {
|
||||
tabs_.CreateInitialTab(nebula::ui::GetHomeUrl());
|
||||
}
|
||||
tabs_.CreateInitialTab(nebula::ui::GetHomeUrl());
|
||||
} else {
|
||||
tabs_.CreateInitialTab(initial_url_);
|
||||
}
|
||||
PersistSession();
|
||||
|
||||
CreateChromeBrowser();
|
||||
CreateContentBrowser();
|
||||
@@ -303,6 +366,12 @@ void NebulaController::OnChromeCommand(const std::string& command, const std::st
|
||||
CloseMenuPopup();
|
||||
} else if (command == "home") {
|
||||
tabs_.LoadURL(nebula::ui::GetHomeUrl());
|
||||
} else if (command == "clear-site-history") {
|
||||
site_history_.clear();
|
||||
SaveSiteHistory(site_history_);
|
||||
if (auto* tab = tabs_.ActiveTab(); tab && tab->browser) {
|
||||
InjectSettingsHistory(tab->browser);
|
||||
}
|
||||
} else if (command == "minimize" && window_) {
|
||||
window_->Minimize();
|
||||
} else if (command == "maximize" && window_) {
|
||||
@@ -315,10 +384,12 @@ void NebulaController::OnChromeCommand(const std::string& command, const std::st
|
||||
}
|
||||
|
||||
void NebulaController::OnContentAddressChanged(CefRefPtr<CefBrowser> browser, const std::string& url) {
|
||||
const std::string internal_url = nebula::ui::ToInternalUrl(url);
|
||||
tabs_.UpdateURL(browser,
|
||||
nebula::ui::IsChromiumNewTabUrl(url)
|
||||
? nebula::ui::GetHomeUrl()
|
||||
: nebula::ui::ToInternalUrl(url));
|
||||
: internal_url);
|
||||
RecordSiteHistory(internal_url);
|
||||
PersistSession();
|
||||
}
|
||||
|
||||
@@ -339,6 +410,12 @@ void NebulaController::OnContentLoadProgressChanged(CefRefPtr<CefBrowser> browse
|
||||
tabs_.UpdateLoadProgress(browser, progress);
|
||||
}
|
||||
|
||||
void NebulaController::OnContentLoadFinished(CefRefPtr<CefBrowser> browser, const std::string& url) {
|
||||
if (nebula::ui::ToInternalUrl(url).starts_with(nebula::ui::GetSettingsUrl())) {
|
||||
InjectSettingsHistory(browser);
|
||||
}
|
||||
}
|
||||
|
||||
void NebulaController::OnContentFaviconChanged(CefRefPtr<CefBrowser> browser, const std::vector<std::string>& urls) {
|
||||
tabs_.UpdateFavicon(browser, urls);
|
||||
}
|
||||
@@ -614,6 +691,33 @@ void NebulaController::SendChromeState(const nebula::browser::NebulaTab& tab) {
|
||||
chrome_browser_->GetMainFrame()->ExecuteJavaScript(script, nebula::ui::GetChromeUrl(), 0);
|
||||
}
|
||||
|
||||
void NebulaController::RecordSiteHistory(const std::string& url) {
|
||||
if (!IsSiteHistoryUrl(url)) {
|
||||
return;
|
||||
}
|
||||
|
||||
site_history_.erase(
|
||||
std::remove(site_history_.begin(), site_history_.end(), url),
|
||||
site_history_.end());
|
||||
site_history_.insert(site_history_.begin(), url);
|
||||
if (site_history_.size() > kMaxSiteHistoryEntries) {
|
||||
site_history_.resize(kMaxSiteHistoryEntries);
|
||||
}
|
||||
SaveSiteHistory(site_history_);
|
||||
}
|
||||
|
||||
void NebulaController::InjectSettingsHistory(CefRefPtr<CefBrowser> browser) {
|
||||
if (!browser) {
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string history_json = SiteHistoryJson(site_history_);
|
||||
const std::string script =
|
||||
"localStorage.setItem('siteHistory', \"" + nebula::browser::JsonEscape(history_json) + "\");"
|
||||
"if (typeof loadHistories === 'function') { loadHistories(); }";
|
||||
browser->GetMainFrame()->ExecuteJavaScript(script, nebula::ui::GetSettingsUrl(), 0);
|
||||
}
|
||||
|
||||
void NebulaController::PersistSession() const {
|
||||
nebula::browser::SaveSessionState(tabs_.Tabs(), tabs_.ActiveTabIndex());
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ public:
|
||||
void OnContentTitleChanged(CefRefPtr<CefBrowser> browser, const std::string& title) override;
|
||||
void OnContentLoadingStateChanged(CefRefPtr<CefBrowser> browser, bool is_loading) override;
|
||||
void OnContentLoadProgressChanged(CefRefPtr<CefBrowser> browser, double progress) override;
|
||||
void OnContentLoadFinished(CefRefPtr<CefBrowser> browser, const std::string& url) override;
|
||||
void OnContentFaviconChanged(CefRefPtr<CefBrowser> browser, const std::vector<std::string>& urls) override;
|
||||
void OnContentFullscreenChanged(CefRefPtr<CefBrowser> browser, bool fullscreen) override;
|
||||
void OnPopupRequested(CefRefPtr<CefBrowser> browser, const std::string& target_url) override;
|
||||
@@ -54,6 +55,8 @@ private:
|
||||
void SetContentFullscreen(bool fullscreen);
|
||||
void ResizeBrowsers();
|
||||
void SendChromeState(const nebula::browser::NebulaTab& tab);
|
||||
void RecordSiteHistory(const std::string& url);
|
||||
void InjectSettingsHistory(CefRefPtr<CefBrowser> browser);
|
||||
void PersistSession() const;
|
||||
void MaybeFinishShutdown();
|
||||
|
||||
@@ -72,6 +75,7 @@ private:
|
||||
CefRefPtr<nebula::cef::NebulaBrowserClient> content_client_;
|
||||
CefRefPtr<nebula::cef::NebulaBrowserClient> menu_popup_client_;
|
||||
std::unordered_set<std::string> insecure_warning_bypasses_;
|
||||
std::vector<std::string> site_history_;
|
||||
};
|
||||
|
||||
} // namespace nebula::app
|
||||
|
||||
Reference in New Issue
Block a user