From 1773cc749b3f63e379bc69559b60d79c7945ece3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Wed, 23 Feb 2022 13:59:33 +0000 Subject: [PATCH] Bug 1756083 - Cleanup snap name handling and enable portal when running under either flatpak or snap. r=stransky Differential Revision: https://phabricator.services.mozilla.com/D139474 --- .../components/shell/nsGNOMEShellService.cpp | 21 ++++----------- toolkit/profile/nsToolkitProfileService.cpp | 19 +++++++------- widget/WidgetUtils.cpp | 9 ------- widget/WidgetUtils.h | 5 ---- widget/gtk/WaylandBuffer.cpp | 7 +++-- widget/gtk/WidgetUtilsGtk.cpp | 26 ++++++++++++++++--- widget/gtk/WidgetUtilsGtk.h | 21 ++++++++++++++- 7 files changed, 59 insertions(+), 49 deletions(-) diff --git a/browser/components/shell/nsGNOMEShellService.cpp b/browser/components/shell/nsGNOMEShellService.cpp index 7c0d5a6fcebf..b1afe232b85a 100644 --- a/browser/components/shell/nsGNOMEShellService.cpp +++ b/browser/components/shell/nsGNOMEShellService.cpp @@ -24,6 +24,7 @@ #include "imgIContainer.h" #include "mozilla/Sprintf.h" #include "mozilla/WidgetUtils.h" +#include "mozilla/WidgetUtilsGtk.h" #include "mozilla/dom/Element.h" #if defined(MOZ_WIDGET_GTK) # include "nsImageToPixbuf.h" @@ -72,18 +73,6 @@ static const MimeTypeAssociation appTypes[] = { #define kDesktopDrawBGGSKey "draw-background" #define kDesktopColorGSKey "primary-color" -static bool IsRunningAsASnap() { - const char* snapName = mozilla::widget::WidgetUtils::GetSnapInstanceName(); - - // return early if not set. - if (snapName == nullptr) { - return false; - } - - // snapName as defined on https://snapcraft.io/firefox - return (strcmp(snapName, "firefox") == 0); -} - nsresult nsGNOMEShellService::Init() { nsresult rv; @@ -200,9 +189,9 @@ nsGNOMEShellService::IsDefaultBrowser(bool aForAllTypes, bool* aIsDefaultBrowser) { *aIsDefaultBrowser = false; - if (IsRunningAsASnap()) { + if (widget::IsRunningUnderSnap()) { const gchar* argv[] = {"xdg-settings", "check", "default-web-browser", - "firefox.desktop", nullptr}; + (SNAP_INSTANCE_NAME ".desktop"), nullptr}; GSpawnFlags flags = static_cast(G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL); gchar* output = nullptr; @@ -280,9 +269,9 @@ nsGNOMEShellService::SetDefaultBrowser(bool aClaimAllTypes, bool aForAllUsers) { "Setting the default browser for all users is not yet supported"); #endif - if (IsRunningAsASnap()) { + if (widget::IsRunningUnderSnap()) { const gchar* argv[] = {"xdg-settings", "set", "default-web-browser", - "firefox.desktop", nullptr}; + (SNAP_INSTANCE_NAME ".desktop"), nullptr}; GSpawnFlags flags = static_cast(G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL); diff --git a/toolkit/profile/nsToolkitProfileService.cpp b/toolkit/profile/nsToolkitProfileService.cpp index 1d0e1e5ec6da..f734061fdbd1 100644 --- a/toolkit/profile/nsToolkitProfileService.cpp +++ b/toolkit/profile/nsToolkitProfileService.cpp @@ -33,6 +33,10 @@ # include "nsILocalFileMac.h" #endif +#ifdef MOZ_WIDGET_GTK +#include "mozilla/WidgetUtilsGtk.h" +#endif + #include "nsAppDirectoryServiceDefs.h" #include "nsDirectoryServiceDefs.h" #include "nsNetCID.h" @@ -1906,16 +1910,11 @@ nsToolkitProfileService::CreateProfile(nsIFile* aRootDir, * get essentially the same benefits as dedicated profiles provides. */ bool nsToolkitProfileService::IsSnapEnvironment() { - const char* snapName = mozilla::widget::WidgetUtils::GetSnapInstanceName(); - - // return early if not set. - if (snapName == nullptr) { - return false; - } - - // snapName as defined on e.g. - // https://snapcraft.io/firefox or https://snapcraft.io/thunderbird - return (strcmp(snapName, MOZ_APP_NAME) == 0); +#ifdef MOZ_WIDGET_GTK + return widget::IsRunningUnderSnap(); +#else + return false; +#endif } /** diff --git a/widget/WidgetUtils.cpp b/widget/WidgetUtils.cpp index f00f10d79e78..3bafdd0a4d27 100644 --- a/widget/WidgetUtils.cpp +++ b/widget/WidgetUtils.cpp @@ -130,14 +130,5 @@ void WidgetUtils::GetBrandShortName(nsAString& aBrandName) { } } -const char* WidgetUtils::GetSnapInstanceName() { - char* instanceName = PR_GetEnv("SNAP_INSTANCE_NAME"); - if (instanceName != nullptr) { - return instanceName; - } - // Compatibility for snapd <= 2.35: - return PR_GetEnv("SNAP_NAME"); -} - } // namespace widget } // namespace mozilla diff --git a/widget/WidgetUtils.h b/widget/WidgetUtils.h index 54a7f3d7fb8c..1971c3854c6e 100644 --- a/widget/WidgetUtils.h +++ b/widget/WidgetUtils.h @@ -88,11 +88,6 @@ class WidgetUtils { * Get branchShortName from string bundle */ static void GetBrandShortName(nsAString& aBrandName); - - /* 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(); }; } // namespace widget diff --git a/widget/gtk/WaylandBuffer.cpp b/widget/gtk/WaylandBuffer.cpp index 632d7d5287e8..7255474fbd85 100644 --- a/widget/gtk/WaylandBuffer.cpp +++ b/widget/gtk/WaylandBuffer.cpp @@ -12,7 +12,7 @@ #include "gfx2DGlue.h" #include "gfxPlatform.h" -#include "mozilla/WidgetUtils.h" // For WidgetUtils +#include "mozilla/WidgetUtilsGtk.h" #include "mozilla/gfx/Tools.h" #include "nsPrintfCString.h" #include "prenv.h" // For PR_GetEnv @@ -44,9 +44,8 @@ char* WaylandBufferSHM::mDumpDir = PR_GetEnv("MOZ_WAYLAND_DUMP_DIR"); static int WaylandAllocateShmMemory(int aSize) { int fd = -1; - nsCString shmPrefix("/"); - const char* snapName = mozilla::widget::WidgetUtils::GetSnapInstanceName(); - if (snapName != nullptr) { + nsAutoCString shmPrefix("/"); + if (const char* snapName = GetSnapInstanceName()) { shmPrefix.AppendPrintf("snap.%s.", snapName); } shmPrefix.Append("wayland.mozilla.ipc"); diff --git a/widget/gtk/WidgetUtilsGtk.cpp b/widget/gtk/WidgetUtilsGtk.cpp index c39b730650ad..c21ef3d27835 100644 --- a/widget/gtk/WidgetUtilsGtk.cpp +++ b/widget/gtk/WidgetUtilsGtk.cpp @@ -98,23 +98,41 @@ bool IsRunningUnderFlatpak() { return sRunning; } +bool IsRunningUnderSnap() { + static bool sRunning = [] { + const char* instanceName = [] { + if (const char* instanceName = g_getenv("SNAP_INSTANCE_NAME")) { + return instanceName; + } + // Compatibility for snapd <= 2.35: + return g_getenv("SNAP_NAME"); + }(); + return instanceName && !strcmp(instanceName, SNAP_INSTANCE_NAME); + }; + return sRunning; +} + +const char* GetSnapInstanceName() { + return IsRunningUnderSnap() ? SNAP_INSTANCE_NAME : nullptr; +} + bool ShouldUsePortal(PortalKind aPortalKind) { - static bool sFlatpakPortalEnv = [] { - if (IsRunningUnderFlatpak()) { + static bool sPortalEnv = [] { + if (IsRunningUnderFlatpakOrSnap()) { return true; } const char* portalEnvString = g_getenv("GTK_USE_PORTAL"); return portalEnvString && atoi(portalEnvString) != 0; }(); - bool autoBehavior = sFlatpakPortalEnv; + bool autoBehavior = sPortalEnv; const int32_t pref = [&] { switch (aPortalKind) { case PortalKind::FilePicker: return StaticPrefs::widget_use_xdg_desktop_portal_file_picker(); case PortalKind::MimeHandler: // Mime portal breaks default browser handling, see bug 1516290. - autoBehavior = IsRunningUnderFlatpak(); + autoBehavior = IsRunningUnderFlatpakOrSnap(); return StaticPrefs::widget_use_xdg_desktop_portal_mime_handler(); case PortalKind::Print: // Print portal still needs more work, so auto behavior is just when diff --git a/widget/gtk/WidgetUtilsGtk.h b/widget/gtk/WidgetUtilsGtk.h index 36ad9f6a0478..ac2d00227c7a 100644 --- a/widget/gtk/WidgetUtilsGtk.h +++ b/widget/gtk/WidgetUtilsGtk.h @@ -10,7 +10,9 @@ #include "nsTArray.h" #include -#include + +typedef struct _GdkDisplay GdkDisplay; +typedef struct _GdkDevice GdkDevice; namespace mozilla::widget { @@ -31,6 +33,23 @@ bool GdkIsX11Display(); GdkDevice* GdkGetPointer(); bool IsRunningUnderFlatpak(); + +// When packaged as a snap, strict confinement needs to be accounted for. +// See https://snapcraft.io/docs for details. +// Name as defined on e.g. +// https://snapcraft.io/firefox or https://snapcraft.io/thunderbird +#ifdef MOZ_APP_NAME +# define SNAP_INSTANCE_NAME MOZ_APP_NAME +#endif + +bool IsRunningUnderSnap(); +inline bool IsRunningUnderFlatpakOrSnap() { + return IsRunningUnderFlatpak() || IsRunningUnderSnap(); +} + +// Return the snap's instance name, or null when not running as a snap. +const char* GetSnapInstanceName(); + enum class PortalKind { FilePicker, MimeHandler,