Bug 1757209 - Don't hardcode snap instance name. r=stransky

Differential Revision: https://phabricator.services.mozilla.com/D139761
This commit is contained in:
Emilio Cobos Álvarez 2022-02-28 15:07:16 +00:00
parent 8724285359
commit 74c4e590a6
4 changed files with 22 additions and 41 deletions

View File

@ -189,7 +189,7 @@ nsGNOMEShellService::IsDefaultBrowser(bool aForAllTypes,
if (widget::IsRunningUnderSnap()) {
const gchar* argv[] = {"xdg-settings", "check", "default-web-browser",
(SNAP_INSTANCE_NAME ".desktop"), nullptr};
(MOZ_APP_NAME ".desktop"), nullptr};
GSpawnFlags flags = static_cast<GSpawnFlags>(G_SPAWN_SEARCH_PATH |
G_SPAWN_STDERR_TO_DEV_NULL);
gchar* output = nullptr;
@ -269,7 +269,7 @@ nsGNOMEShellService::SetDefaultBrowser(bool aClaimAllTypes, bool aForAllUsers) {
if (widget::IsRunningUnderSnap()) {
const gchar* argv[] = {"xdg-settings", "set", "default-web-browser",
(SNAP_INSTANCE_NAME ".desktop"), nullptr};
(MOZ_APP_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

@ -18,6 +18,7 @@
#ifdef OS_LINUX
# include "linux_memfd_defs.h"
# include "mozilla/WidgetUtilsGtk.h"
#endif
#ifdef __FreeBSD__
@ -265,17 +266,8 @@ bool SharedMemory::AppendPosixShmPrefix(std::string* str, pid_t pid) {
// (it's used for communication with services like PulseAudio);
// instead AppArmor is used to restrict access to it. Anything with
// this prefix is allowed:
static const char* const kSnap = [] {
auto instanceName = PR_GetEnv("SNAP_INSTANCE_NAME");
if (instanceName != nullptr) {
return instanceName;
}
// Compatibility for snapd <= 2.35:
return PR_GetEnv("SNAP_NAME");
}();
if (kSnap) {
StringAppendF(str, "snap.%s.", kSnap);
if (const char* snap = mozilla::widget::GetSnapInstanceName()) {
StringAppendF(str, "snap.%s.", snap);
}
# endif // OS_LINUX
// Hopefully the "implementation defined" name length limit is long

View File

@ -98,22 +98,21 @@ 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;
static const char* sInstanceName = []() -> const char* {
// Intentionally leaked, as keeping a pointer to the environment forever is
// a bit suspicious.
if (const char* instanceName = g_getenv("SNAP_INSTANCE_NAME")) {
return g_strdup(instanceName);
}
// Compatibility for snapd <= 2.35:
if (const char* instanceName = g_getenv("SNAP_NAME")) {
return g_strdup(instanceName);
}
return nullptr;
}();
return sInstanceName;
}
bool ShouldUsePortal(PortalKind aPortalKind) {

View File

@ -32,24 +32,14 @@ bool GdkIsX11Display();
GdkDevice* GdkGetPointer();
// Return the snap's instance name, or null when not running as a snap.
const char* GetSnapInstanceName();
inline bool IsRunningUnderSnap() { return !!GetSnapInstanceName(); }
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,