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
This commit is contained in:
Emilio Cobos Álvarez 2022-02-23 13:59:33 +00:00
parent f819216a55
commit 0422925d7e
7 changed files with 59 additions and 49 deletions

View File

@ -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<GSpawnFlags>(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<GSpawnFlags>(G_SPAWN_SEARCH_PATH |
G_SPAWN_STDOUT_TO_DEV_NULL |
G_SPAWN_STDERR_TO_DEV_NULL);

View File

@ -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
}
/**

View File

@ -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

View File

@ -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

View File

@ -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");

View File

@ -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

View File

@ -10,7 +10,9 @@
#include "nsTArray.h"
#include <stdint.h>
#include <gdk/gdk.h>
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,