Truly independent web browser
UI/Qt+LibWebView: Ask for notifications with Allow and Never allow
sideshowbarker0dff94a2626a
diff --git a/Libraries/LibWebView/NotificationBackend.h b/Libraries/LibWebView/NotificationBackend.hindex 679699450bc..fe574b8eeda 100644--- a/Libraries/LibWebView/NotificationBackend.h+++ b/Libraries/LibWebView/NotificationBackend.h@@ -55,6 +55,10 @@ public:// Non-null for the backend tests install, which keeps what it was shown.virtual RecordingNotificationBackend* as_recording_backend() { return nullptr; }+ // True while the operating system hasn't yet been asked whether this browser may show notifications at all, so+ // the system will ask the user right after the browser does.+ virtual bool os_authorization_is_undetermined() const { return false; }+Function<void(u64 id)> on_shown;Function<void(u64 id)> on_failed;Function<void(u64 id)> on_clicked;diff --git a/Libraries/LibWebView/NotificationBackendMacOS.h b/Libraries/LibWebView/NotificationBackendMacOS.hindex b976d73c492..5d97ce64fac 100644--- a/Libraries/LibWebView/NotificationBackendMacOS.h+++ b/Libraries/LibWebView/NotificationBackendMacOS.h@@ -34,6 +34,7 @@ public:virtual void show(PlatformNotification const&) override;virtual void close(u64 id) override;+ virtual bool os_authorization_is_undetermined() const override { return m_authorization == Authorization::Unknown; }// What the notification center's delegate reports: the user activated or dismissed a request.void did_activate(String const& identifier);diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cppindex bb16472c958..792cf46aee1 100644--- a/Libraries/LibWebView/WebContentPage.cpp+++ b/Libraries/LibWebView/WebContentPage.cpp@@ -1791,8 +1791,18 @@ void WebContentPage::did_request_permission(u64 request_id, Web::HTML::CrossProc}auto generation_when_asked = manager.decision_generation(*permission_name, *top_level_origin);- // When the system hasn't yet let the browser find the user's location either, the prompt says the system will ask next.- auto show_os_hint = *permission_name == Web::Permissions::PermissionName::Geolocation && Application::the().geolocation_authorization() == Core::GeolocationAuthorization::NotDetermined;+ // When the system hasn't yet let the browser use the feature either, the prompt says the system will ask next.+ auto show_os_hint = [&] {+ switch (*permission_name) {+ case Web::Permissions::PermissionName::Geolocation:+ return Application::the().geolocation_authorization() == Core::GeolocationAuthorization::NotDetermined;+ case Web::Permissions::PermissionName::Notifications:+ return NotificationManager::the().backend().os_authorization_is_undetermined();+ case Web::Permissions::PermissionName::Microphone:+ return false;+ }+ VERIFY_NOT_REACHED();+ }();PermissionPromptRequest request { *permission_name, *top_level_origin, is_file, show_os_hint };view().request_permission_prompt(move(request), [page = NonnullRefPtr(*this), request_id, document_id, permission_name = *permission_name, top_level_origin = *top_level_origin, is_file, generation_when_asked](Web::Permissions::PromptOutcome outcome) {diff --git a/Tests/UI/Qt/TestPermissionPromptPopover.cpp b/Tests/UI/Qt/TestPermissionPromptPopover.cppindex 4edce8dfd8c..1da0e85d47a 100644--- a/Tests/UI/Qt/TestPermissionPromptPopover.cpp+++ b/Tests/UI/Qt/TestPermissionPromptPopover.cpp@@ -136,6 +136,29 @@ TEST_CASE(question_and_answers)EXPECT_EQ(child<QPushButton>(site, "LadybirdPermissionPromptBlock").text(), QStringLiteral("Never allow"));}+TEST_CASE(notifications_prompt_has_two_answers)+{+ PermissionPromptPopover mail(nullptr, request_for("https://mail.example.com/"sv, PermissionName::Notifications));+ EXPECT_EQ(child<QPushButton>(mail, "LadybirdPermissionPromptAllowAlways").text(), QStringLiteral("Allow"));+ EXPECT_EQ(child<QPushButton>(mail, "LadybirdPermissionPromptBlock").text(), QStringLiteral("Never allow"));+ EXPECT(mail.findChild<QPushButton*>(QStringLiteral("LadybirdPermissionPromptAllowOnce")) == nullptr);++ Recorder recorder;+ recorder.attach(mail);+ child<QPushButton>(mail, "LadybirdPermissionPromptAllowAlways").click();+ EXPECT_EQ(recorder.outcomes.size(), 1u);+ EXPECT(recorder.outcomes[0] == PromptOutcome::AllowAlways);+}++TEST_CASE(hint_text_names_the_feature)+{+ PermissionPromptPopover location(nullptr, request_for("https://maps.example.com/"sv, PermissionName::Geolocation, true));+ EXPECT_EQ(child<QLabel>(location, "LadybirdPermissionPromptHint").text(), QStringLiteral("macOS will then ask you to let Ladybird use your location."));++ PermissionPromptPopover notifications(nullptr, request_for("https://mail.example.com/"sv, PermissionName::Notifications, true));+ EXPECT_EQ(child<QLabel>(notifications, "LadybirdPermissionPromptHint").text(), QStringLiteral("macOS will then ask you to let Ladybird show notifications."));+}+TEST_CASE(hint_shows_only_when_requested){PermissionPromptPopover without_hint(nullptr, request_for("https://maps.example.com/"sv));diff --git a/UI/Qt/PermissionPromptPopover.cpp b/UI/Qt/PermissionPromptPopover.cppindex 68c5c1f0660..d2be5016f68 100644--- a/UI/Qt/PermissionPromptPopover.cpp+++ b/UI/Qt/PermissionPromptPopover.cpp@@ -48,6 +48,27 @@ static QString feature_text(Web::Permissions::PermissionName name)VERIFY_NOT_REACHED();}+// Shown while the system hasn't yet been asked whether Ladybird may use the feature: it asks right after this prompt.+static QString hint_text(Web::Permissions::PermissionName name)+{+ switch (name) {+ case Web::Permissions::PermissionName::Geolocation:+ return QStringLiteral("macOS will then ask you to let Ladybird use your location.");+ case Web::Permissions::PermissionName::Microphone:+ return QStringLiteral("macOS will then ask you to let Ladybird use your microphone.");+ case Web::Permissions::PermissionName::Notifications:+ return QStringLiteral("macOS will then ask you to let Ladybird show notifications.");+ }+ VERIFY_NOT_REACHED();+}++// A notification grant has no one-time form: One that lapsed once the site had no visible tab for a while would end+// right when a notification is wanted. So that prompt offers a lasting Allow and Never allow only.+static bool offers_one_time_grant(Web::Permissions::PermissionName name)+{+ return name != Web::Permissions::PermissionName::Notifications;+}+static ChromeIcon feature_icon(Web::Permissions::PermissionName name){switch (name) {@@ -62,7 +83,7 @@ static ChromeIcon feature_icon(Web::Permissions::PermissionName name)}// The prompt is laid out like a message popover: the permission's icon in a tile before the question, and the answers-// below. All three answers are secondary buttons, since the browser recommends none of them.+// below. Every answer is a secondary button, since the browser recommends none of them.PermissionPromptPopover::PermissionPromptPopover(QWidget* parent, WebView::PermissionPromptRequest const& request): Popover(parent), m_request(request)@@ -89,7 +110,7 @@ PermissionPromptPopover::PermissionPromptPopover(QWidget* parent, WebView::Permiheader->addWidget(title, 1);layout->addLayout(header);- auto* hint = new QLabel(QStringLiteral("macOS will then ask you to let Ladybird use your location."), &card());+ auto* hint = new QLabel(hint_text(request.name), &card());hint->setObjectName("LadybirdPermissionPromptHint");hint->setWordWrap(true);hint->setVisible(request.show_os_hint);@@ -105,8 +126,10 @@ PermissionPromptPopover::PermissionPromptPopover(QWidget* parent, WebView::PermiQObject::connect(button, &QPushButton::clicked, this, [this, outcome] { finish(outcome); });layout->addWidget(button);};- add_button("LadybirdPermissionPromptAllowAlways", QStringLiteral("Allow while visiting the site"), Web::Permissions::PromptOutcome::AllowAlways);- add_button("LadybirdPermissionPromptAllowOnce", QStringLiteral("Allow this time"), Web::Permissions::PromptOutcome::AllowOnce);+ auto one_time_grant = offers_one_time_grant(request.name);+ add_button("LadybirdPermissionPromptAllowAlways", one_time_grant ? QStringLiteral("Allow while visiting the site") : QStringLiteral("Allow"), Web::Permissions::PromptOutcome::AllowAlways);+ if (one_time_grant)+ add_button("LadybirdPermissionPromptAllowOnce", QStringLiteral("Allow this time"), Web::Permissions::PromptOutcome::AllowOnce);add_button("LadybirdPermissionPromptBlock", QStringLiteral("Never allow"), Web::Permissions::PromptOutcome::Block);update_icon();