diff --git a/browser/components/BrowserGlue.jsm b/browser/components/BrowserGlue.jsm index ead9c6a7d22b..db8923e705cc 100644 --- a/browser/components/BrowserGlue.jsm +++ b/browser/components/BrowserGlue.jsm @@ -3353,7 +3353,7 @@ BrowserGlue.prototype = { _migrateUI: function BG__migrateUI() { // Use an increasing number to keep track of the current migration state. // Completely unrelated to the current Firefox release number. - const UI_VERSION = 121; + const UI_VERSION = 122; const BROWSER_DOCURL = AppConstants.BROWSER_CHROME_URL; const PROFILE_DIR = Services.dirsvc.get("ProfD", Ci.nsIFile).path; @@ -4055,6 +4055,24 @@ BrowserGlue.prototype = { ); } + if (currentUIVersion < 122) { + // Migrate xdg-desktop-portal pref from old to new prefs. + try { + const oldPref = "widget.use-xdg-desktop-portal"; + if (Services.prefs.getBoolPref(oldPref)) { + Services.prefs.setIntPref( + "widget.use-xdg-desktop-portal.file-picker", + 1 + ); + Services.prefs.setIntPref( + "widget.use-xdg-desktop-portal.mime-handler", + 1 + ); + } + Services.prefs.clearUserPref(oldPref); + } catch (ex) {} + } + // Update the migration version. Services.prefs.setIntPref("browser.migration.version", UI_VERSION); }, diff --git a/modules/libpref/init/StaticPrefList.yaml b/modules/libpref/init/StaticPrefList.yaml index 4840c6cdcb29..94fccc9dfe16 100644 --- a/modules/libpref/init/StaticPrefList.yaml +++ b/modules/libpref/init/StaticPrefList.yaml @@ -12449,6 +12449,36 @@ mirror: once #endif +#ifdef MOZ_WIDGET_GTK +# +# Whether to use gtk portal for the file picker. +# - 0: never +# - 1: always +# - 2: auto (true for flatpak or GTK_USE_PORTAL=1, false otherwise) +- name: widget.use-xdg-desktop-portal.file-picker + type: int32_t + value: 2 + mirror: always + +# Whether to use gtk portal for the mime handler. +# - 0: never +# - 1: always +# - 2: auto (true for flatpak or GTK_USE_PORTAL=1, false otherwise) +- name: widget.use-xdg-desktop-portal.mime-handler + type: int32_t + value: 2 + mirror: always + +# Whether to use gtk portal for the print dialog. +# - 0: never +# - 1: always +# - 2: auto (for now only true for flatpak, since it needs work, see bug 1688720). +- name: widget.use-xdg-desktop-portal.print + type: int32_t + value: 2 + mirror: always +#endif + #ifdef XP_WIN # WindowsUIUtils::Share to wait for user action on Windows share dialog # `true` means the promise resolves when user completes or cancels the share diff --git a/modules/libpref/init/all.js b/modules/libpref/init/all.js index 2e8b09826068..3f32ba1bb051 100644 --- a/modules/libpref/init/all.js +++ b/modules/libpref/init/all.js @@ -3716,9 +3716,6 @@ pref("network.psl.onUpdate_notify", false); pref("widget.disable-workspace-management", false); pref("widget.titlebar-x11-use-shape-mask", false); #endif -#ifdef MOZ_WAYLAND - pref("widget.use-xdg-desktop-portal", false); -#endif // All the Geolocation preferences are here. // diff --git a/toolkit/modules/Troubleshoot.jsm b/toolkit/modules/Troubleshoot.jsm index fe4b87256e1d..7a210f693cf9 100644 --- a/toolkit/modules/Troubleshoot.jsm +++ b/toolkit/modules/Troubleshoot.jsm @@ -105,6 +105,9 @@ const PREFS_FOR_DISPLAY = [ "webgl.", "widget.dmabuf", "widget.use-xdg-desktop-portal", + "widget.use-xdg-desktop-portal.file-picker", + "widget.use-xdg-desktop-portal.mime-handler", + "widget.use-xdg-desktop-portal.print", "widget.wayland", ]; diff --git a/toolkit/system/gnome/nsGIOService.cpp b/toolkit/system/gnome/nsGIOService.cpp index 612619ea7a1e..f0389349db59 100644 --- a/toolkit/system/gnome/nsGIOService.cpp +++ b/toolkit/system/gnome/nsGIOService.cpp @@ -402,7 +402,7 @@ nsGIOService::GetAppForURIScheme(const nsACString& aURIScheme, // Application in flatpak sandbox does not have access to the list // of installed applications on the system. We use generic // nsFlatpakHandlerApp which forwards launch call to the system. - if (widget::ShouldUsePortal()) { + if (widget::ShouldUsePortal(widget::PortalKind::MimeHandler)) { nsFlatpakHandlerApp* mozApp = new nsFlatpakHandlerApp(); NS_ADDREF(*aApp = mozApp); return NS_OK; @@ -460,7 +460,7 @@ nsGIOService::GetAppForMimeType(const nsACString& aMimeType, // Flatpak does not reveal installed application to the sandbox, // we need to create generic system handler. - if (widget::ShouldUsePortal()) { + if (widget::ShouldUsePortal(widget::PortalKind::MimeHandler)) { nsFlatpakHandlerApp* mozApp = new nsFlatpakHandlerApp(); NS_ADDREF(*aApp = mozApp); return NS_OK; diff --git a/widget/gtk/WidgetUtilsGtk.cpp b/widget/gtk/WidgetUtilsGtk.cpp index 23c919021376..68ed1f1d0982 100644 --- a/widget/gtk/WidgetUtilsGtk.cpp +++ b/widget/gtk/WidgetUtilsGtk.cpp @@ -5,7 +5,7 @@ #include "WidgetUtilsGtk.h" -#include "mozilla/Preferences.h" +#include "mozilla/StaticPrefs_widget.h" #include "mozilla/UniquePtr.h" #include "nsReadableUtils.h" #include "nsWindow.h" @@ -92,7 +92,7 @@ bool IsRunningUnderFlatpak() { return sRunning; } -bool ShouldUsePortal() { +bool ShouldUsePortal(PortalKind aPortalKind) { static bool sFlatpakPortalEnv = [] { if (IsRunningUnderFlatpak()) { return true; @@ -100,9 +100,31 @@ bool ShouldUsePortal() { const char* portalEnvString = g_getenv("GTK_USE_PORTAL"); return portalEnvString && atoi(portalEnvString) != 0; }(); - return Preferences::HasUserValue("widget.use-xdg-desktop-portal") - ? Preferences::GetBool("widget.use-xdg-desktop-portal", false) - : sFlatpakPortalEnv; + + bool autoBehavior = sFlatpakPortalEnv; + const int32_t pref = [&] { + switch (aPortalKind) { + case PortalKind::FilePicker: + return StaticPrefs::widget_use_xdg_desktop_portal_file_picker(); + case PortalKind::MimeHandler: + return StaticPrefs::widget_use_xdg_desktop_portal_mime_handler(); + case PortalKind::Print: + // Print portal still needs more work, so auto behavior is just when + // flatpak is enabled. + autoBehavior = IsRunningUnderFlatpak(); + return StaticPrefs::widget_use_xdg_desktop_portal_print(); + } + return 2; + }(); + + switch (pref) { + case 0: + return false; + case 1: + return true; + default: + return autoBehavior; + } } nsTArray ParseTextURIList(const nsACString& aData) { diff --git a/widget/gtk/WidgetUtilsGtk.h b/widget/gtk/WidgetUtilsGtk.h index 27da73d4337b..c0d116e08cf8 100644 --- a/widget/gtk/WidgetUtilsGtk.h +++ b/widget/gtk/WidgetUtilsGtk.h @@ -29,7 +29,12 @@ bool GdkIsWaylandDisplay(); bool GdkIsX11Display(); bool IsRunningUnderFlatpak(); -bool ShouldUsePortal(); +enum class PortalKind { + FilePicker, + MimeHandler, + Print, +}; +bool ShouldUsePortal(PortalKind); // Parse text/uri-list nsTArray ParseTextURIList(const nsACString& data); diff --git a/widget/gtk/nsDeviceContextSpecG.cpp b/widget/gtk/nsDeviceContextSpecG.cpp index 9c75536f6a04..b1aafa04f3fe 100644 --- a/widget/gtk/nsDeviceContextSpecG.cpp +++ b/widget/gtk/nsDeviceContextSpecG.cpp @@ -275,7 +275,9 @@ gboolean nsDeviceContextSpecGTK::PrinterEnumerator(GtkPrinter* aPrinter, void nsDeviceContextSpecGTK::StartPrintJob() { // When using flatpak, we have to call the Print method of the portal - if (widget::ShouldUsePortal()) { + // + // FIXME: This code doesn't seem to be working alright, see bug 1688720. + if (widget::ShouldUsePortal(widget::PortalKind::Print)) { GError* error = nullptr; GDBusProxy* dbusProxy = g_dbus_proxy_new_for_bus_sync( G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, nullptr, @@ -291,6 +293,7 @@ void nsDeviceContextSpecGTK::StartPrintJob() { int fd = open(mSpoolName.get(), O_RDONLY | O_CLOEXEC); if (fd == -1) { NS_WARNING("Failed to open spool file."); + g_object_unref(dbusProxy); return; } static auto s_g_unix_fd_list_new = reinterpret_cast( @@ -306,10 +309,9 @@ void nsDeviceContextSpecGTK::StartPrintJob() { close(fd); // We'll pass empty options as long as we don't have token from PreparePrint - // dbus call (which we don't use). This unfortunatelly lead to showing - // gtk print dialog and also the duplex or printer specific settings - // is not honored, so this needs to be fixed when the portal provides - // more options. + // dbus call (which we don't use). This unfortunately leads to showing gtk + // print dialog and also the duplex or printer specific settings is not + // honored, so this needs to be fixed when the portal provides more options. GVariantBuilder opt_builder; g_variant_builder_init(&opt_builder, G_VARIANT_TYPE_VARDICT); @@ -411,7 +413,7 @@ NS_IMETHODIMP nsDeviceContextSpecGTK::EndDocument() { destFile->SetPermissions(0666 & ~(mask)); // Notify flatpak printing portal that file is completely written - if (widget::ShouldUsePortal()) { + if (widget::ShouldUsePortal(widget::PortalKind::Print)) { // Use the name of the file for printing to match with // nsFlatpakPrintPortal nsCOMPtr os = diff --git a/widget/gtk/nsFilePicker.cpp b/widget/gtk/nsFilePicker.cpp index b0cd105cb001..00a5caa288ff 100644 --- a/widget/gtk/nsFilePicker.cpp +++ b/widget/gtk/nsFilePicker.cpp @@ -171,14 +171,8 @@ nsFilePicker::nsFilePicker() mRunning(false), mAllowURLs(false), mFileChooserDelegate(nullptr) { - // Due to Bug 1635718 always use portal for file dialog on Wayland. - // FIXME: This is not what this code is doing? Pref is default false. - if (widget::GdkIsWaylandDisplay()) { - mUseNativeFileChooser = - Preferences::GetBool("widget.use-xdg-desktop-portal", true); - } else { - mUseNativeFileChooser = widget::ShouldUsePortal(); - } + mUseNativeFileChooser = + widget::ShouldUsePortal(widget::PortalKind::FilePicker); } nsFilePicker::~nsFilePicker() = default; diff --git a/widget/gtk/nsPrintDialogGTK.cpp b/widget/gtk/nsPrintDialogGTK.cpp index d8459a1b5b5d..8fb56543b71e 100644 --- a/widget/gtk/nsPrintDialogGTK.cpp +++ b/widget/gtk/nsPrintDialogGTK.cpp @@ -909,7 +909,8 @@ nsPrintDialogServiceGTK::Show(nsPIDOMWindowOuter* aParent, MOZ_ASSERT(aSettings, "aSettings must not be null"); // Check for the flatpak portal first - if (ShouldUsePortal() && gtk_check_version(3, 22, 0) == nullptr) { + if (ShouldUsePortal(widget::PortalKind::Print) && + gtk_check_version(3, 22, 0) == nullptr) { nsCOMPtr widget = WidgetUtils::DOMWindowToWidget(aParent); NS_ASSERTION(widget, "Need a widget for dialog to be modal."); GtkWindow* gtkParent = get_gtk_window_for_nsiwidget(widget);