From 7b5c4d8ee0213c22c2559b7c502c64850397cdcf Mon Sep 17 00:00:00 2001 From: Olivier Tilloy Date: Wed, 7 Apr 2021 20:55:42 +0000 Subject: [PATCH] Bug 1701182 - Prepend SNAP-specific prefix to shm path. r=stransky Differential Revision: https://phabricator.services.mozilla.com/D110269 --- .../components/shell/nsGNOMEShellService.cpp | 11 +++++----- toolkit/profile/moz.build | 3 +++ toolkit/profile/nsToolkitProfileService.cpp | 21 +++++++++++++------ widget/gtk/WidgetUtilsGtk.cpp | 10 +++++++++ widget/gtk/WidgetUtilsGtk.h | 5 +++++ widget/gtk/WindowSurfaceWayland.cpp | 11 +++++++++- 6 files changed, 48 insertions(+), 13 deletions(-) diff --git a/browser/components/shell/nsGNOMEShellService.cpp b/browser/components/shell/nsGNOMEShellService.cpp index b6313bafedf6..bb95c362c91b 100644 --- a/browser/components/shell/nsGNOMEShellService.cpp +++ b/browser/components/shell/nsGNOMEShellService.cpp @@ -23,6 +23,7 @@ #include "imgIRequest.h" #include "imgIContainer.h" #include "mozilla/Sprintf.h" +#include "mozilla/WidgetUtilsGtk.h" #include "mozilla/dom/Element.h" #if defined(MOZ_WIDGET_GTK) # include "nsIImageToPixbuf.h" @@ -72,17 +73,15 @@ static const MimeTypeAssociation appTypes[] = { #define kDesktopColorGSKey "primary-color" static bool IsRunningAsASnap() { - // SNAP holds the path to the snap, use SNAP_NAME - // which is easier to parse. - const char* snap_name = PR_GetEnv("SNAP_NAME"); + const char* snapName = mozilla::widget::WidgetUtilsGTK::GetSnapInstanceName(); // return early if not set. - if (snap_name == nullptr) { + if (snapName == nullptr) { return false; } - // snap_name as defined on https://snapcraft.io/firefox - return (strcmp(snap_name, "firefox") == 0); + // snapName as defined on https://snapcraft.io/firefox + return (strcmp(snapName, "firefox") == 0); } nsresult nsGNOMEShellService::Init() { diff --git a/toolkit/profile/moz.build b/toolkit/profile/moz.build index db4df9abb2a7..37cc406882ce 100644 --- a/toolkit/profile/moz.build +++ b/toolkit/profile/moz.build @@ -30,6 +30,9 @@ if CONFIG["OS_TARGET"] == "Android": UNIFIED_SOURCES += ["nsToolkitProfileService.cpp"] +if CONFIG["MOZ_WIDGET_TOOLKIT"] == "gtk": + CXXFLAGS += CONFIG["MOZ_GTK3_CFLAGS"] + LOCAL_INCLUDES += [ "../xre", ] diff --git a/toolkit/profile/nsToolkitProfileService.cpp b/toolkit/profile/nsToolkitProfileService.cpp index 9dabfae37601..363a0a075c31 100644 --- a/toolkit/profile/nsToolkitProfileService.cpp +++ b/toolkit/profile/nsToolkitProfileService.cpp @@ -50,6 +50,10 @@ #include "mozilla/Telemetry.h" #include "nsProxyRelease.h" +#if defined(MOZ_WIDGET_GTK) +# include "mozilla/WidgetUtilsGtk.h" +#endif + using namespace mozilla; #define DEV_EDITION_NAME "dev-edition-default" @@ -1833,14 +1837,19 @@ nsToolkitProfileService::CreateProfile(nsIFile* aRootDir, * get essentially the same benefits as dedicated profiles provides. */ bool nsToolkitProfileService::IsSnapEnvironment() { - // Copied from IsRunningAsASnap() in - // browser/components/shell/nsGNOMEShellService.cpp - // TODO: factor out this common code in one place. - const char* snap_name = PR_GetEnv("SNAP_NAME"); - if (snap_name == nullptr) { +#if defined(MOZ_WIDGET_GTK) + const char* snapName = mozilla::widget::WidgetUtilsGTK::GetSnapInstanceName(); + + // return early if not set. + if (snapName == nullptr) { return false; } - return (strcmp(snap_name, "firefox") == 0); + + // snapName as defined on https://snapcraft.io/firefox + return (strcmp(snapName, "firefox") == 0); +#endif + + return false; } /** diff --git a/widget/gtk/WidgetUtilsGtk.cpp b/widget/gtk/WidgetUtilsGtk.cpp index 66e764e26799..940d2a93f1b5 100644 --- a/widget/gtk/WidgetUtilsGtk.cpp +++ b/widget/gtk/WidgetUtilsGtk.cpp @@ -5,6 +5,7 @@ #include "WidgetUtilsGtk.h" #include "nsWindow.h" +#include "prenv.h" #include #include @@ -44,6 +45,15 @@ int32_t WidgetUtilsGTK::IsTouchDeviceSupportPresent() { return result; } +const char* WidgetUtilsGTK::GetSnapInstanceName() { + char* instanceName = PR_GetEnv("SNAP_INSTANCE_NAME"); + if (instanceName != nullptr) { + return instanceName; + } + // Compatibility for snapd <= 2.35: + return PR_GetEnv("SNAP_NAME"); +} + bool IsMainWindowTransparent() { return nsWindow::IsToplevelWindowTransparent(); } diff --git a/widget/gtk/WidgetUtilsGtk.h b/widget/gtk/WidgetUtilsGtk.h index bc3393644b52..2dc0cafbcae1 100644 --- a/widget/gtk/WidgetUtilsGtk.h +++ b/widget/gtk/WidgetUtilsGtk.h @@ -16,6 +16,11 @@ class WidgetUtilsGTK { public: /* See WidgetUtils::IsTouchDeviceSupportPresent(). */ static int32_t IsTouchDeviceSupportPresent(); + + /* When packaged as a snap, strict confinement needs to be accounted for. + See https://snapcraft.io/docs for details. + Return the snap's instance name, or null when not running as a snap. */ + static const char* GetSnapInstanceName(); }; bool IsMainWindowTransparent(); diff --git a/widget/gtk/WindowSurfaceWayland.cpp b/widget/gtk/WindowSurfaceWayland.cpp index f11af4ebec2f..1c2135e15fbc 100644 --- a/widget/gtk/WindowSurfaceWayland.cpp +++ b/widget/gtk/WindowSurfaceWayland.cpp @@ -5,6 +5,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "nsWaylandDisplay.h" +#include "WidgetUtilsGtk.h" #include "WindowSurfaceWayland.h" #include "nsPrintfCString.h" @@ -169,9 +170,17 @@ RefPtr WindowBackBuffer::GetWaylandDisplay() { static int WaylandAllocateShmMemory(int aSize) { int fd = -1; + + nsCString shmPrefix("/"); + const char* snapName = mozilla::widget::WidgetUtilsGTK::GetSnapInstanceName(); + if (snapName != nullptr) { + shmPrefix.AppendPrintf("snap.%s.", snapName); + } + shmPrefix.Append("wayland.mozilla.ipc"); + do { static int counter = 0; - nsPrintfCString shmName("/wayland.mozilla.ipc.%d", counter++); + nsPrintfCString shmName("%s.%d", shmPrefix.get(), counter++); fd = shm_open(shmName.get(), O_CREAT | O_RDWR | O_EXCL, 0600); if (fd >= 0) { // We don't want to use leaked file