Skip to content

Truly independent web browser

LibWebView: Offer back only the windows the last run left open

Shannon Boothe7f8ea11d482
diff --git a/Libraries/LibWebView/SessionStore.cpp b/Libraries/LibWebView/SessionStore.cpp
index d309639545e..b36cf210549 100644
--- a/Libraries/LibWebView/SessionStore.cpp
+++ b/Libraries/LibWebView/SessionStore.cpp
@@ -331,6 +331,7 @@ ErrorOr<SessionStore::PersistedStorage> SessionStore::PersistedStorage::create(D
statements.select_open_sessions = TRY(database.prepare_statement("SELECT id, active_tab_index, activation_sequence, x, y, width, height, maximized FROM Sessions WHERE kind = :open_window_kind ORDER BY activation_sequence, id;"sv));
statements.select_restore_at_launch_session = TRY(database.prepare_statement("SELECT id, active_tab_index, activation_sequence, x, y, width, height, maximized FROM Sessions WHERE kind = :closed_window_kind AND restore_at_launch != 0 ORDER BY close_sequence DESC, id DESC LIMIT 1;"sv));
statements.clear_restore_at_launch = TRY(database.prepare_statement("UPDATE Sessions SET restore_at_launch = 0 WHERE restore_at_launch != 0;"sv));
+ statements.clear_startup_recovery_origin = TRY(database.prepare_statement("UPDATE Sessions SET origin = :close_action_origin WHERE origin = :startup_recovery_origin;"sv));
statements.reopen_closed_session = TRY(database.prepare_statement("UPDATE Sessions SET kind = :open_window_kind, closed_time = 0, close_sequence = 0, source_window_id = NULL, source_tab_index = NULL, origin = :origin WHERE id = :id;"sv));
statements.recover_open_session = TRY(database.prepare_statement("UPDATE Sessions SET kind = :closed_window_kind, closed_time = :closed_time, close_sequence = :close_sequence, source_window_id = NULL, source_tab_index = NULL, origin = :origin WHERE id = :id;"sv));
statements.delete_empty_open_sessions = TRY(database.prepare_statement("DELETE FROM Sessions WHERE kind = :open_window_kind AND NOT EXISTS (SELECT 1 FROM SessionTabs WHERE session_id = Sessions.id);"sv));
@@ -873,6 +874,15 @@ ErrorOr<void> SessionStore::PersistedStorage::clear_restore_at_launch()
return database->try_execute_bound_statement(statements.clear_restore_at_launch, [](auto&) -> ErrorOr<void> { return {}; });
}
+ErrorOr<void> SessionStore::PersistedStorage::clear_startup_recovery_origin()
+{
+ return database->try_execute_bound_statement(statements.clear_startup_recovery_origin, [](auto& bind) -> ErrorOr<void> {
+ TRY(bind("close_action_origin"sv, to_underlying(ClosedSessionUnit::Origin::CloseAction)));
+ TRY(bind("startup_recovery_origin"sv, to_underlying(ClosedSessionUnit::Origin::StartupRecovery)));
+ return {};
+ });
+}
+
ErrorOr<void> SessionStore::PersistedStorage::demote_to_startup_recovery_unit(SessionWindowId window_id, UnixDateTime recovered_at, IdAllocator& close_sequence_allocator)
{
auto close_sequence = TRY(close_sequence_allocator.allocate());
@@ -957,6 +967,10 @@ ErrorOr<SessionStore::RecoveryResult> SessionStore::PersistedStorage::recover_op
result.consecutive_unclean_exits = TRY(record_run_start());
auto policy = result.consecutive_unclean_exits >= UNCLEAN_EXITS_THAT_STOP_LAUNCH_RESTORE ? SessionLaunchPolicy::None : write.policy;
+ // Only the windows that the last run left open count as left open. Those of the runs before it were already
+ // offered back, so they are ordinary closed windows now.
+ TRY(clear_startup_recovery_origin());
+
TRY(database->try_execute_bound_statement(statements.delete_empty_open_sessions, [&](auto& bind) -> ErrorOr<void> {
return bind("open_window_kind"sv, to_underlying(UnitKind::OpenWindow));
}));
diff --git a/Libraries/LibWebView/SessionStore.h b/Libraries/LibWebView/SessionStore.h
index 6ef2470fee0..fa7932bbf09 100644
--- a/Libraries/LibWebView/SessionStore.h
+++ b/Libraries/LibWebView/SessionStore.h
@@ -64,7 +64,7 @@ struct RecentlyClosedSessionUnit {
URL::URL active_url;
size_t tab_count { 0 };
bool was_window { false };
- // Left open by a run that ended without closing it, rather than closed by the user.
+ // Left open by the last run, which ended without closing it, rather than closed by the user.
bool from_startup_recovery { false };
UnixDateTime closed_at;
};
@@ -252,6 +252,7 @@ private:
Database::StatementID select_open_sessions;
Database::StatementID select_restore_at_launch_session;
Database::StatementID clear_restore_at_launch;
+ Database::StatementID clear_startup_recovery_origin;
Database::StatementID reopen_closed_session;
Database::StatementID recover_open_session;
Database::StatementID delete_empty_open_sessions;
@@ -427,6 +428,7 @@ private:
ErrorOr<void> clear_closed_units(ClearClosedUnitsWrite const&);
ErrorOr<RecoveryResult> recover_open_windows(RecoveryWrite const&, IdAllocator& session_id_allocator, IdAllocator& close_sequence_allocator);
ErrorOr<void> clear_restore_at_launch();
+ ErrorOr<void> clear_startup_recovery_origin();
ErrorOr<void> delete_session_rows(SessionWindowId);
ErrorOr<void> prune_closed_units(UnitKind, u32 max_units);
ErrorOr<void> write_lifecycle(bool clean_exit, u32 consecutive_unclean_exits);
diff --git a/Tests/LibWebView/TestSessionStore.cpp b/Tests/LibWebView/TestSessionStore.cpp
index 9b4d06e84d7..c77733dfbae 100644
--- a/Tests/LibWebView/TestSessionStore.cpp
+++ b/Tests/LibWebView/TestSessionStore.cpp
@@ -1710,6 +1710,25 @@ TEST_CASE(a_second_unclean_exit_in_a_row_stops_launch_restore)
EXPECT_EQ(unit->origin, ClosedSessionUnit::Origin::StartupRecovery);
}
+TEST_CASE(only_the_last_run_counts_as_leaving_windows_open)
+{
+ auto database = create_session_database();
+ // Two crashes in a row, each with a window of its own.
+ for (auto url : { "https://a.example/"sv, "https://b.example/"sv }) {
+ auto store = TRY_OR_FAIL(SessionStore::create(*database));
+ auto window = TRY_OR_FAIL(store->window_opened());
+ (void)open_tab(*store, window, url);
+ }
+
+ auto store = TRY_OR_FAIL(SessionStore::create(*database));
+ auto units = store->recently_closed_units(10);
+ EXPECT_EQ(units.size(), 2uz);
+ EXPECT_EQ(units[0].active_url.serialize(), "https://b.example/"sv);
+ EXPECT(units[0].from_startup_recovery);
+ EXPECT_EQ(units[1].active_url.serialize(), "https://a.example/"sv);
+ EXPECT(!units[1].from_startup_recovery);
+}
+
TEST_CASE(a_clean_exit_resumes_launch_restore)
{
auto database = create_session_database();