fix: Use XDG_ACTIVATION_TOKEN in wayland when launched by other app (#43546)

When an electron app is launched by another app ensure that the
XDG_ACTIVATION_TOKEN env var is read and used for activation using
xdg_activation_v1 protocol.

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Orko Garai <orko@igalia.com>
This commit is contained in:
trop[bot] 2024-09-04 17:28:06 +02:00 committed by GitHub
parent 0ee624c308
commit 6172e1d2b9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 0 deletions

View file

@ -406,6 +406,11 @@ std::optional<int> ElectronMainDelegate::PreBrowserMain() {
content::InitializeMojoCore(); content::InitializeMojoCore();
#if BUILDFLAG(IS_MAC) #if BUILDFLAG(IS_MAC)
RegisterAtomCrApp(); RegisterAtomCrApp();
#endif
#if BUILDFLAG(IS_LINUX)
// Set the global activation token sent as an environment variable.
auto env = base::Environment::Create();
base::nix::ExtractXdgActivationTokenFromEnv(*env);
#endif #endif
return std::nullopt; return std::nullopt;
} }

View file

@ -92,6 +92,11 @@
#include "shell/browser/ui/cocoa/electron_bundle_mover.h" #include "shell/browser/ui/cocoa/electron_bundle_mover.h"
#endif #endif
#if BUILDFLAG(IS_LINUX)
#include "base/nix/scoped_xdg_activation_token_injector.h"
#include "base/nix/xdg_util.h"
#endif
using electron::Browser; using electron::Browser;
namespace gin { namespace gin {
@ -411,6 +416,12 @@ bool NotificationCallbackWrapper(
base::CommandLine cmd, base::CommandLine cmd,
const base::FilePath& cwd, const base::FilePath& cwd,
const std::vector<uint8_t> additional_data) { const std::vector<uint8_t> additional_data) {
#if BUILDFLAG(IS_LINUX)
// Set the global activation token sent as a command line switch by another
// electron app instance. This also removes the switch after use to prevent
// any side effects of leaving it in the command line after this point.
base::nix::ExtractXdgActivationTokenFromCmdLine(cmd);
#endif
// Make sure the callback is called after app gets ready. // Make sure the callback is called after app gets ready.
if (Browser::Get()->is_ready()) { if (Browser::Get()->is_ready()) {
callback.Run(std::move(cmd), cwd, std::move(additional_data)); callback.Run(std::move(cmd), cwd, std::move(additional_data));
@ -1022,6 +1033,15 @@ bool App::RequestSingleInstanceLock(gin::Arguments* args) {
base::BindRepeating(NotificationCallbackWrapper, cb)); base::BindRepeating(NotificationCallbackWrapper, cb));
#endif #endif
#if BUILDFLAG(IS_LINUX)
// Read the xdg-activation token and set it in the command line for the
// duration of the notification in order to ensure this is propagated to an
// already running electron app instance if it exists.
// The activation token received from the launching app is used later when
// activating the existing electron app instance window.
base::nix::ScopedXdgActivationTokenInjector activation_token_injector(
*base::CommandLine::ForCurrentProcess(), *base::Environment::Create());
#endif
switch (process_singleton_->NotifyOtherProcessOrCreate()) { switch (process_singleton_->NotifyOtherProcessOrCreate()) {
case ProcessSingleton::NotifyResult::PROCESS_NONE: case ProcessSingleton::NotifyResult::PROCESS_NONE:
if (content::BrowserThread::IsThreadInitialized( if (content::BrowserThread::IsThreadInitialized(