From 8cca75da5cb2f5ffc03cd3578422d4bb0c58f462 Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 8 Oct 2021 13:46:52 +0400 Subject: [PATCH] Handle foreign instance in COM toast activator. --- .../win/notifications_manager_win.cpp | 27 ++++++++++++++++--- .../SourceFiles/platform/win/specific_win.cpp | 8 ------ .../platform/win/windows_toast_activator.cpp | 3 +++ 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/Telegram/SourceFiles/platform/win/notifications_manager_win.cpp b/Telegram/SourceFiles/platform/win/notifications_manager_win.cpp index 422fd023d..a1d79b582 100644 --- a/Telegram/SourceFiles/platform/win/notifications_manager_win.cpp +++ b/Telegram/SourceFiles/platform/win/notifications_manager_win.cpp @@ -18,6 +18,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "platform/win/windows_toast_activator.h" #include "platform/win/windows_event_filter.h" #include "platform/win/windows_dlls.h" +#include "platform/win/specific_win.h" #include "history/history.h" #include "core/application.h" #include "core/core_settings.h" @@ -549,17 +550,34 @@ void Manager::Private::clearNotification(NotificationId id) { void Manager::Private::handleActivation(const ToastActivation &activation) { const auto parsed = qthelp::url_parse_params(activation.args); + const auto pid = parsed.value("pid").toULong(); + const auto my = GetCurrentProcessId(); + if (pid != my) { + DEBUG_LOG(("Toast Info: " + "Got activation \"%1\", my %2, activating %3." + ).arg(activation.args + ).arg(my + ).arg(pid)); + psActivateProcess(pid); + return; + } const auto action = parsed.value("action"); const auto id = NotificationId{ .full = FullPeer{ - .sessionId = parsed.value("s").toULongLong(), - .peerId = PeerId(parsed.value("p").toULongLong()), + .sessionId = parsed.value("session").toULongLong(), + .peerId = PeerId(parsed.value("peer").toULongLong()), }, - .msgId = MsgId(parsed.value("m").toLongLong()), + .msgId = MsgId(parsed.value("msg").toLongLong()), }; if (!id.full.sessionId || !id.full.peerId || !id.msgId) { + DEBUG_LOG(("Toast Info: Got activation \"%1\", my %1, skipping." + ).arg(activation.args + ).arg(pid)); return; } + DEBUG_LOG(("Toast Info: Got activation \"%1\", my %1, handling." + ).arg(activation.args + ).arg(pid)); auto text = TextWithTags(); for (const auto &entry : activation.input) { if (entry.key == "fastReply") { @@ -633,7 +651,8 @@ bool Manager::Private::showNotificationInTryCatch( .full = key, .msgId = msgId }; - const auto idString = u"s=%1&p=%2&m=%3"_q + const auto idString = u"pid=%1&session=%2&peer=%3&msg=%4"_q + .arg(GetCurrentProcessId()) .arg(key.sessionId) .arg(key.peerId.value) .arg(msgId.bare); diff --git a/Telegram/SourceFiles/platform/win/specific_win.cpp b/Telegram/SourceFiles/platform/win/specific_win.cpp index 0624ca8c3..c6e752c49 100644 --- a/Telegram/SourceFiles/platform/win/specific_win.cpp +++ b/Telegram/SourceFiles/platform/win/specific_win.cpp @@ -146,14 +146,6 @@ void DeleteMyModules() { void psActivateProcess(uint64 pid) { if (pid) { ::EnumWindows((WNDENUMPROC)_ActivateProcess, (LPARAM)&pid); - } else if (Core::IsAppLaunched()) { - if (const auto window = Core::App().activeWindow()) { - if (const auto handle = window->widget()->windowHandle()) { - if (const auto id = handle->winId()) { - SetForegroundWindow(HWND(id)); - } - } - } } } diff --git a/Telegram/SourceFiles/platform/win/windows_toast_activator.cpp b/Telegram/SourceFiles/platform/win/windows_toast_activator.cpp index 44ac8f553..415d4c7b1 100644 --- a/Telegram/SourceFiles/platform/win/windows_toast_activator.cpp +++ b/Telegram/SourceFiles/platform/win/windows_toast_activator.cpp @@ -36,6 +36,9 @@ HRESULT ToastActivator::Activate( _In_ LPCWSTR invokedArgs, _In_reads_(dataCount) const NOTIFICATION_USER_INPUT_DATA *data, ULONG dataCount) { + DEBUG_LOG(("Toast Info: COM Activated \"%1\" with args \"%2\"." + ).arg(QString::fromWCharArray(appUserModelId) + ).arg(QString::fromWCharArray(invokedArgs))); const auto string = &ToastActivation::String; auto input = std::vector(); input.reserve(dataCount);