From a25b2e9700a2d06d91e32d37bf51955eb80b819f Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 30 Jul 2024 15:53:06 +0200 Subject: [PATCH] Show webview error in Iv::Controller. --- Telegram/SourceFiles/iv/iv_controller.cpp | 68 +++++++++++++++++-- Telegram/SourceFiles/iv/iv_controller.h | 3 + .../payments/ui/payments_panel.cpp | 30 ++------ .../settings/settings_experimental.cpp | 3 +- .../ui/chat/attach/attach_bot_webview.cpp | 36 +++++----- .../ui/chat/attach/attach_bot_webview.h | 2 + 6 files changed, 91 insertions(+), 51 deletions(-) diff --git a/Telegram/SourceFiles/iv/iv_controller.cpp b/Telegram/SourceFiles/iv/iv_controller.cpp index 1870011dc..487f9295c 100644 --- a/Telegram/SourceFiles/iv/iv_controller.cpp +++ b/Telegram/SourceFiles/iv/iv_controller.cpp @@ -11,8 +11,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/invoke_queued.h" #include "base/qt_signal_producer.h" #include "base/qthelp_url.h" +#include "core/file_utilities.h" #include "iv/iv_data.h" #include "lang/lang_keys.h" +#include "ui/chat/attach/attach_bot_webview.h" #include "ui/platform/ui_platform_window_title.h" #include "ui/widgets/buttons.h" #include "ui/widgets/labels.h" @@ -28,6 +30,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "styles/palette.h" #include "styles/style_iv.h" #include "styles/style_menu_icons.h" +#include "styles/style_payments.h" // paymentsCriticalError #include "styles/style_widgets.h" #include "styles/style_window.h" @@ -194,7 +197,7 @@ Controller::~Controller() { _window->hide(); } _ready = false; - _webview = nullptr; + base::take(_webview); _back.destroy(); _forward.destroy(); _menu = nullptr; @@ -335,8 +338,6 @@ void Controller::showTonSite( if (_webview && _webview->widget()) { _webview->navigate(url); activate(); - } else { - _events.fire({ Event::Type::Close }); } _url = url; _subtitleText = _url.value( @@ -435,7 +436,7 @@ void Controller::createWebview(const Webview::StorageId &storageId) { window->lifetime().add([=] { _ready = false; - _webview = nullptr; + base::take(_webview); }); window->events( @@ -449,11 +450,32 @@ void Controller::createWebview(const Webview::StorageId &storageId) { } } }, window->lifetime()); - raw->widget()->show(); + + const auto widget = raw->widget(); + if (!widget) { + base::take(_webview); + showWebviewError(); + return; + } + widget->show(); + + QObject::connect(widget, &QObject::destroyed, [=] { + if (!_webview) { + // If we destroyed _webview ourselves, + // we don't show any message, nothing crashed. + return; + } + crl::on_main(window, [=] { + showWebviewError({ "Error: WebView has crashed." }); + }); + base::take(_webview); + }); _container->sizeValue( ) | rpl::start_with_next([=](QSize size) { - raw->widget()->setGeometry(QRect(QPoint(), size)); + if (const auto widget = raw->widget()) { + widget->setGeometry(QRect(QPoint(), size)); + } }, _container->lifetime()); raw->setNavigationStartHandler([=](const QString &uri, bool newWindow) { @@ -600,11 +622,45 @@ void Controller::createWebview(const Webview::StorageId &storageId) { _back->setAttribute( Qt::WA_TransparentForMouseEvents, !state.canGoBack); + _url = QString::fromStdString(state.url); }, _webview->lifetime()); raw->init(R"()"); } +void Controller::showWebviewError() { + const auto available = Webview::Availability(); + if (available.error != Webview::Available::Error::None) { + showWebviewError(Ui::BotWebView::ErrorText(available)); + } else { + showWebviewError({ "Error: Could not initialize WebView." }); + } +} + +void Controller::showWebviewError(TextWithEntities text) { + auto error = Ui::CreateChild>( + _container, + object_ptr( + _container, + rpl::single(text), + st::paymentsCriticalError), + st::paymentsCriticalErrorPadding); + error->entity()->setClickHandlerFilter([=]( + const ClickHandlerPtr &handler, + Qt::MouseButton) { + const auto entity = handler->getTextEntity(); + if (entity.type != EntityType::CustomUrl) { + return true; + } + File::OpenUrl(entity.data); + return false; + }); + error->show(); + _container->sizeValue() | rpl::start_with_next([=](QSize size) { + error->setGeometry(0, 0, size.width(), size.height() * 2 / 3); + }, error->lifetime()); +} + void Controller::showInWindow( const Webview::StorageId &storageId, Prepared page) { diff --git a/Telegram/SourceFiles/iv/iv_controller.h b/Telegram/SourceFiles/iv/iv_controller.h index b732ec57a..c92f4c4b2 100644 --- a/Telegram/SourceFiles/iv/iv_controller.h +++ b/Telegram/SourceFiles/iv/iv_controller.h @@ -123,6 +123,9 @@ private: void showShareMenu(); void destroyShareMenu(); + void showWebviewError(); + void showWebviewError(TextWithEntities text); + const not_null _delegate; std::unique_ptr _window; diff --git a/Telegram/SourceFiles/payments/ui/payments_panel.cpp b/Telegram/SourceFiles/payments/ui/payments_panel.cpp index d97628285..2bc023e8c 100644 --- a/Telegram/SourceFiles/payments/ui/payments_panel.cpp +++ b/Telegram/SourceFiles/payments/ui/payments_panel.cpp @@ -16,6 +16,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/widgets/checkbox.h" #include "ui/wrap/fade_wrap.h" #include "ui/boxes/single_choice_box.h" +#include "ui/chat/attach/attach_bot_webview.h" #include "ui/text/format_values.h" #include "ui/text/text_utilities.h" #include "ui/effects/radial_animation.h" @@ -908,32 +909,9 @@ std::shared_ptr Panel::uiShow() { void Panel::showWebviewError( const QString &text, const Webview::Available &information) { - using Error = Webview::Available::Error; - Expects(information.error != Error::None); - - auto rich = TextWithEntities{ text }; - rich.append("\n\n"); - switch (information.error) { - case Error::NoWebview2: { - rich.append(tr::lng_payments_webview_install_edge( - tr::now, - lt_link, - Text::Link( - "Microsoft Edge WebView2 Runtime", - "https://go.microsoft.com/fwlink/p/?LinkId=2124703"), - Ui::Text::WithEntities)); - } break; - case Error::NoWebKitGTK: - rich.append(tr::lng_payments_webview_install_webkit(tr::now)); - break; - case Error::OldWindows: - rich.append(tr::lng_payments_webview_update_windows(tr::now)); - break; - default: - rich.append(QString::fromStdString(information.details)); - break; - } - showCriticalError(rich); + showCriticalError(TextWithEntities{ text }.append( + "\n\n" + ).append(BotWebView::ErrorText(information))); } void Panel::updateThemeParams(const Webview::ThemeParams ¶ms) { diff --git a/Telegram/SourceFiles/settings/settings_experimental.cpp b/Telegram/SourceFiles/settings/settings_experimental.cpp index 69e0ef89e..c17eda5b8 100644 --- a/Telegram/SourceFiles/settings/settings_experimental.cpp +++ b/Telegram/SourceFiles/settings/settings_experimental.cpp @@ -24,7 +24,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "lang/lang_keys.h" #include "mainwindow.h" #include "media/player/media_player_instance.h" -#include "webview/platform/win/webview_windows_edge_chromium.h" #include "webview/webview_embed.h" #include "window/main_window.h" #include "window/window_peer_menu.h" @@ -150,7 +149,7 @@ void SetupExperimental( addToggle(Media::Player::kOptionDisableAutoplayNext); addToggle(kOptionSendLargePhotos); addToggle(Webview::kOptionWebviewDebugEnabled); - addToggle(Webview::EdgeChromium::kOptionWebviewLegacyEdge); + addToggle(Webview::kOptionWebviewLegacyEdge); addToggle(kOptionAutoScrollInactiveChat); addToggle(Window::Notifications::kOptionGNotification); addToggle(Core::kOptionFreeType); diff --git a/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.cpp b/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.cpp index b2159ebb1..7b57464f7 100644 --- a/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.cpp +++ b/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.cpp @@ -1401,32 +1401,34 @@ if (window.TelegramGameProxy) { )"); } -void Panel::showWebviewError( - const QString &text, - const Webview::Available &information) { - using Error = Webview::Available::Error; - Expects(information.error != Error::None); +TextWithEntities ErrorText(const Webview::Available &info) { + Expects(info.error != Webview::Available::Error::None); - auto rich = TextWithEntities{ text }; - rich.append("\n\n"); - switch (information.error) { - case Error::NoWebview2: { - rich.append(tr::lng_payments_webview_install_edge( + using Error = Webview::Available::Error; + switch (info.error) { + case Error::NoWebview2: + return tr::lng_payments_webview_install_edge( tr::now, lt_link, Text::Link( "Microsoft Edge WebView2 Runtime", "https://go.microsoft.com/fwlink/p/?LinkId=2124703"), - Ui::Text::WithEntities)); - } break; + Ui::Text::WithEntities); case Error::NoWebKitGTK: - rich.append(tr::lng_payments_webview_install_webkit(tr::now)); - break; + return { tr::lng_payments_webview_install_webkit(tr::now) }; + case Error::OldWindows: + return { tr::lng_payments_webview_update_windows(tr::now) }; default: - rich.append(QString::fromStdString(information.details)); - break; + return { QString::fromStdString(info.details) }; } - showCriticalError(rich); +} + +void Panel::showWebviewError( + const QString &text, + const Webview::Available &information) { + showCriticalError(TextWithEntities{ text }.append( + "\n\n" + ).append(ErrorText(information))); } rpl::lifetime &Panel::lifetime() { diff --git a/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.h b/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.h index e25483b89..f687c24e6 100644 --- a/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.h +++ b/Telegram/SourceFiles/ui/chat/attach/attach_bot_webview.h @@ -30,6 +30,8 @@ struct Available; namespace Ui::BotWebView { +[[nodiscard]] TextWithEntities ErrorText(const Webview::Available &info); + struct MainButtonArgs { bool isActive = false; bool isVisible = false;