Bug 1746559 - Split flatpak portal prefs. r=jhorak

This allows to test and enable the different portals separately. Make
the print portal work only when actually running under flatpak by
default, given it needs more work as per the regressing bug.

And while at it fix a minor leak and some typos in that code.

Differential Revision: https://phabricator.services.mozilla.com/D135120
This commit is contained in:
Emilio Cobos Álvarez 2022-01-05 14:55:56 +00:00
parent 6d97f61bd1
commit 4079907232
10 changed files with 99 additions and 27 deletions

View File

@ -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);
},

View File

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

View File

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

View File

@ -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",
];

View File

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

View File

@ -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<nsCString> ParseTextURIList(const nsACString& aData) {

View File

@ -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<nsCString> ParseTextURIList(const nsACString& data);

View File

@ -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<GUnixFDList* (*)(void)>(
@ -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<nsIObserverService> os =

View File

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

View File

@ -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<nsIWidget> widget = WidgetUtils::DOMWindowToWidget(aParent);
NS_ASSERTION(widget, "Need a widget for dialog to be modal.");
GtkWindow* gtkParent = get_gtk_window_for_nsiwidget(widget);