Skip to content

Truly independent web browser

LibWebView+UI/Qt: Keep a notification within its own browsing session

sideshowbarker332f79d2bcfb
diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.h
index d85bfd06367..4521391925f 100644
--- a/Libraries/LibWebView/Application.h
+++ b/Libraries/LibWebView/Application.h
@@ -162,7 +162,7 @@ public:
virtual void focus_page_from_notification(WebContentPage&) { }
// A click on a notification whose page is gone: Open the URL in a new foreground tab of that window, if it's
// still open, or of the active window.
- virtual void open_url_in_new_tab_from_notification(URL::URL const&, [[maybe_unused]] Web::PageId originating_page, CanonicalTraversable*) { }
+ virtual void open_url_in_new_tab_from_notification(URL::URL const&, [[maybe_unused]] Web::PageId originating_page, CanonicalTraversable*, IsPrivate) { }
void appearance_changed(Badge<ApplicationSettingsObserver>);
void tab_settings_changed(Badge<ApplicationSettingsObserver>);
diff --git a/Libraries/LibWebView/NotificationManager.cpp b/Libraries/LibWebView/NotificationManager.cpp
index c4ff0677521..15dcdcc912c 100644
--- a/Libraries/LibWebView/NotificationManager.cpp
+++ b/Libraries/LibWebView/NotificationManager.cpp
@@ -70,6 +70,7 @@ void NotificationManager::show(WebContentPage& page, u64 page_notification_id, U
.origin_url = URL::Parser::basic_parse(origin.serialize()).value_or(url),
.traversable = page.traversable().make_weak_ptr<CanonicalTraversable>(),
.tag = data.tag,
+ .is_private = page.view().session().is_private(),
};
// Same origin and same tag take the place of the earlier notification (the spec's oldNotification): Its page
@@ -78,7 +79,7 @@ void NotificationManager::show(WebContentPage& page, u64 page_notification_id, U
if (!data.tag.is_empty()) {
for (size_t index = 0; index < m_records.size(); ++index) {
auto const& existing = m_records[index];
- if (existing.tag == data.tag && existing.origin.is_same_origin(origin)) {
+ if (existing.tag == data.tag && existing.is_private == record.is_private && existing.origin.is_same_origin(origin)) {
replaces = existing.id;
report_closed(existing, false);
m_records.remove(index);
@@ -90,12 +91,12 @@ void NotificationManager::show(WebContentPage& page, u64 page_notification_id, U
// An origin shows a bounded number of notifications at a time; the oldest makes room for a new one.
size_t live_for_origin = 0;
for (auto const& existing : m_records) {
- if (existing.origin.is_same_origin(origin))
+ if (existing.is_private == record.is_private && existing.origin.is_same_origin(origin))
++live_for_origin;
}
while (live_for_origin >= max_live_notifications_per_origin) {
for (size_t index = 0; index < m_records.size(); ++index) {
- if (m_records[index].origin.is_same_origin(origin)) {
+ if (m_records[index].is_private == record.is_private && m_records[index].origin.is_same_origin(origin)) {
close_record_from_browser(index);
--live_for_origin;
break;
@@ -246,8 +247,9 @@ void NotificationManager::backend_did_click(u64 id)
auto origin_url = record.origin_url;
auto page_id = record.page_id;
auto traversable = record.traversable;
+ auto is_private = record.is_private;
m_records.remove(*index);
- Application::the().open_url_in_new_tab_from_notification(origin_url, page_id, traversable.ptr());
+ Application::the().open_url_in_new_tab_from_notification(origin_url, page_id, traversable.ptr(), is_private);
}
void NotificationManager::backend_did_close(u64 id, NotificationBackend::ByUser by_user)
diff --git a/Libraries/LibWebView/NotificationManager.h b/Libraries/LibWebView/NotificationManager.h
index efd9d90f214..c55d180e090 100644
--- a/Libraries/LibWebView/NotificationManager.h
+++ b/Libraries/LibWebView/NotificationManager.h
@@ -18,6 +18,7 @@
#include <LibURL/URL.h>
#include <LibWebCommon/NotificationsAPI/NotificationData.h>
#include <LibWebCommon/Page/PageId.h>
+#include <LibWebView/BrowsingSession.h>
#include <LibWebView/Forward.h>
#include <LibWebView/NotificationBackend.h>
@@ -65,6 +66,9 @@ private:
URL::URL origin_url;
WeakPtr<CanonicalTraversable> traversable;
Utf16String tag;
+ // Tag replacement and the per-origin cap never reach across sessions, so a private window's notification
+ // can't close a normal window's and the other way around.
+ IsPrivate is_private { IsPrivate::No };
};
NotificationManager() = default;
diff --git a/Tests/LibWeb/test-web/Application.cpp b/Tests/LibWeb/test-web/Application.cpp
index cc8b94140f2..af4b7030b8f 100644
--- a/Tests/LibWeb/test-web/Application.cpp
+++ b/Tests/LibWeb/test-web/Application.cpp
@@ -153,7 +153,7 @@ void Application::focus_page_from_notification(WebView::WebContentPage& page)
recording_backend->record_focus(page.id());
}
-void Application::open_url_in_new_tab_from_notification(URL::URL const& url, Web::PageId originating_page, WebView::CanonicalTraversable*)
+void Application::open_url_in_new_tab_from_notification(URL::URL const& url, Web::PageId originating_page, WebView::CanonicalTraversable*, WebView::IsPrivate)
{
if (auto* recording_backend = WebView::NotificationManager::the().backend().as_recording_backend())
recording_backend->record_open(url, originating_page);
diff --git a/Tests/LibWeb/test-web/Application.h b/Tests/LibWeb/test-web/Application.h
index c97a2d21801..db016854051 100644
--- a/Tests/LibWeb/test-web/Application.h
+++ b/Tests/LibWeb/test-web/Application.h
@@ -39,7 +39,7 @@ public:
// browser's windows is recorded there too.
virtual NonnullOwnPtr<WebView::NotificationBackend> create_notification_backend() override;
virtual void focus_page_from_notification(WebView::WebContentPage&) override;
- virtual void open_url_in_new_tab_from_notification(URL::URL const&, Web::PageId originating_page, WebView::CanonicalTraversable*) override;
+ virtual void open_url_in_new_tab_from_notification(URL::URL const&, Web::PageId originating_page, WebView::CanonicalTraversable*, WebView::IsPrivate) override;
ErrorOr<void> launch_test_fixtures();
diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cpp
index f98349e3f12..3cbce0aa63a 100644
--- a/UI/Qt/Application.cpp
+++ b/UI/Qt/Application.cpp
@@ -1325,17 +1325,29 @@ void Application::focus_page_from_notification(WebView::WebContentPage& page)
window->bring_to_front();
}
-void Application::open_url_in_new_tab_from_notification(URL::URL const& url, Web::PageId, WebView::CanonicalTraversable* traversable)
+void Application::open_url_in_new_tab_from_notification(URL::URL const& url, Web::PageId, WebView::CanonicalTraversable* traversable, WebView::IsPrivate is_private)
{
BrowserWindow* window = nullptr;
if (traversable) {
if (auto view = traversable->view(); view.has_value())
window = window_for_view(*view);
}
- if (!window)
+ // The site opens in the notification's own session: a private window's notification never lands in a normal
+ // window, nor a normal one in a private window.
+ if (window && window->is_private() != is_private)
+ window = nullptr;
+ if (!window && m_active_window && m_active_window->is_private() == is_private)
window = m_active_window;
if (!window) {
- new_window({ url });
+ for (auto* widget : QApplication::topLevelWidgets()) {
+ if (auto* candidate = qobject_cast<BrowserWindow*>(widget); candidate && candidate->is_private() == is_private) {
+ window = candidate;
+ break;
+ }
+ }
+ }
+ if (!window) {
+ new_window({ url }, configuration_for_new_window(), BrowserWindow::IsPopupWindow::No, is_private);
return;
}
window->new_tab_from_url(url, Web::HTML::ActivateTab::Yes, BrowserWindow::TabLocation::end());
diff --git a/UI/Qt/Application.h b/UI/Qt/Application.h
index 03df8ba6125..c6dec74cba3 100644
--- a/UI/Qt/Application.h
+++ b/UI/Qt/Application.h
@@ -147,7 +147,7 @@ private:
virtual NonnullOwnPtr<WebView::NotificationBackend> create_notification_backend() override;
virtual void focus_page_from_notification(WebView::WebContentPage&) override;
- virtual void open_url_in_new_tab_from_notification(URL::URL const&, Web::PageId originating_page, WebView::CanonicalTraversable*) override;
+ virtual void open_url_in_new_tab_from_notification(URL::URL const&, Web::PageId originating_page, WebView::CanonicalTraversable*, WebView::IsPrivate) override;
virtual bool supports_picture_in_picture() const override { return true; }
virtual OwnPtr<WebView::PictureInPictureWindow> create_picture_in_picture_window(WebView::WebContentPage& requesting_page, WebView::CanonicalTraversable&, Gfx::IntSize video_size) override;