Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background portal (Rebased 12-03-2024) #70

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

joshuamegnauth54
Copy link
Contributor

@joshuamegnauth54 joshuamegnauth54 commented Jul 28, 2024

Requires the access portal to be enabled or else it does nothing

I based my implementation on the official specs as well as code from GNOME, KDE, Xapp (Xfce), and Pantheon (elementary). KDE's immaculately readable codebase served as this implementation's primary inspiration.

Like KDE, I intend to show a dialog, so the user may choose whether to grant privileges to an application. I will also provide an option to bypass the dialog for greater choice especially considering that GNOME automatically grants permissions.

According to the docs, the Autostart method is deprecated. However, I think I still need to implement it because background requests aren't routed to the COSMIC portal unless it's available. Autostart is implemented

Closes: #49

@joshuamegnauth54 joshuamegnauth54 force-pushed the feat-background-autorun-portal branch 10 times, most recently from 8a4ca82 to 5d6e3f2 Compare August 4, 2024 11:39
@joshuamegnauth54 joshuamegnauth54 force-pushed the feat-background-autorun-portal branch 3 times, most recently from c725ec4 to 57e06f4 Compare August 8, 2024 06:42
@joshuamegnauth54 joshuamegnauth54 force-pushed the feat-background-autorun-portal branch 3 times, most recently from 9a93471 to d2d7b90 Compare August 18, 2024 05:48
@joshuamegnauth54 joshuamegnauth54 force-pushed the feat-background-autorun-portal branch from d2d7b90 to 0e5eeb2 Compare September 4, 2024 05:34
@joshuamegnauth54 joshuamegnauth54 force-pushed the feat-background-autorun-portal branch 4 times, most recently from 78be554 to b1026bb Compare September 11, 2024 02:07
@ids1024
Copy link
Member

ids1024 commented Sep 11, 2024

According to the docs, the Autostart method is deprecated. However, I think I still need to implement it because background requests aren't routed to the COSMIC portal unless it's available.

Looks like it was deprecated in May (flatpak/xdg-desktop-portal@458560c) and the last release was in April. So presumably it's still required on released versions of xdg-desktop-portal, but won't in the next.

@joshuamegnauth54 joshuamegnauth54 force-pushed the feat-background-autorun-portal branch 3 times, most recently from c82b8ad to ff66c14 Compare September 18, 2024 06:41
@joshuamegnauth54
Copy link
Contributor Author

Their reasoning makes sense. Every portal backend I checked implements autostart the same way (which, in a way, was really nice because it made it easier to implement here 😂 ).

This is finally done and it works too! I learned a lot in the process as well.

@joshuamegnauth54 joshuamegnauth54 marked this pull request as ready for review September 18, 2024 06:53
@joshuamegnauth54 joshuamegnauth54 changed the title WIP Background portal Background portal Sep 18, 2024
// xxx Is this the correct way to determine if a program is running in the
// background? If a toplevel exists but isn't running, activated, et cetera,
// then it logically must be in the background (?)
AppStatus::Background
Copy link
Member

@Drakulix Drakulix Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is right. A toplevel is a window, this state seems to describe applications without a window. So the logic should be: If any toplevel exists, then the app is either Running or Active. We currently don't have a protocol to enumerate applications without a window.

I would think the portal would have to track apps launched in the background.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review and help!

I checked how GNOME handles GetAppState. GNOME exposes a D-Bus interface that allows retrieving the list of running applications. It also has a signal for app updates.

Its Background portal uses that interface to check app statuses. An app that is running without windows is in the background. Apps with windows have their toplevel status checked (active, running).

KDE doesn't seem to check if a program is in the background at all. 🤔 I find this confusing.

The code for the GNOME background portal is pretty straightforward, but I'll have to check if there is a way to get running apps easily via COSMIC.

Copy link
Member

@Drakulix Drakulix Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't because we don't track "running applications" (which KDE probably also isn't doing), because doing so isn't exactly easy. Any running binary could be an "app".

I assume what Gnome is doing internally is:

  1. Handle flatpaks as apps
  2. Use app-scopes to identify apps it launched through one of it's interfaces (still doesn't help with stuff started e.g. via a terminal).

You can do similar things on cosmic, as we also set the corresponding app-scopes. (e.g. systemctl --user list-units -t scope | grep -e "app-flatpak" -e "app-cosmic")

You would use the systemd dbus interface in the portal to enumerate the scopes, filter for app-cosmic and app-flatpak and pull the app_id from the name (the third component). Afterwards you can lookup the desktop-file, if you need anything more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again for the help! I think this is ready again. I'm actively working on this patch again, so any future changes should be implemented faster. 😁

@joshuamegnauth54 joshuamegnauth54 marked this pull request as draft September 19, 2024 02:01
@joshuamegnauth54 joshuamegnauth54 force-pushed the feat-background-autorun-portal branch 3 times, most recently from e05a724 to fbe43f9 Compare October 11, 2024 06:17
@joshuamegnauth54 joshuamegnauth54 marked this pull request as ready for review October 11, 2024 06:17
@joshuamegnauth54 joshuamegnauth54 marked this pull request as draft October 11, 2024 06:21
@joshuamegnauth54 joshuamegnauth54 force-pushed the feat-background-autorun-portal branch from fbe43f9 to 2d371c1 Compare October 13, 2024 04:52
@joshuamegnauth54 joshuamegnauth54 marked this pull request as ready for review October 13, 2024 05:57
@joshuamegnauth54 joshuamegnauth54 force-pushed the feat-background-autorun-portal branch from 2d371c1 to 77b1577 Compare October 13, 2024 11:59
@gabriele2000
Copy link

gabriele2000 commented Nov 5, 2024

Can't wait until this is approved!

Would this also fix cosmic-files not being able to supply files for flatpak uploads?
For example: uploading a file using cosmic-file as picker in firefox (flatpak)

@joshuamegnauth54
Copy link
Contributor Author

Nope! But I can try to look into that after finishing some other patches I'm working on. 😺

The background portal allows sandboxed apps permission to run in the background without any windows open.

@gabriele2000
Copy link

Nope! But I can try to look into that after finishing some other patches I'm working on. 😺

The background portal allows sandboxed apps permission to run in the background without any windows open.

I'll try to provide logs to help with the implementation

@joshuamegnauth54
Copy link
Contributor Author

Sounds good. I'll take a look into it soon. I can't guarantee I'll be able to fix it though. 😆

@gabriele2000
Copy link

gabriele2000 commented Nov 7, 2024

Sounds good. I'll take a look into it soon. I can't guarantee I'll be able to fix it though. 😆

Here's the spam, it could be useful

nov 07 14:07:28 msi-gp72m dbus-daemon[2061]: [session uid=1000 pid=2059] Activating service name='org.freedesktop.systemd1' requested by ':1.126' (uid=1000 pid=2154 comm="cosmic-launcher")
nov 07 14:07:28 msi-gp72m dbus-daemon[2061]: [session uid=1000 pid=2059] Activated service 'org.freedesktop.systemd1' failed: Process org.freedesktop.systemd1 exited with status 1
nov 07 14:07:28 msi-gp72m dbus-daemon[2061]: [session uid=1000 pid=2059] Activating service name='org.freedesktop.Flatpak' requested by ':1.128' (uid=1000 pid=4045 comm="/usr/bin/flatpak run --branch=stable --arch=x86_64")
nov 07 14:07:28 msi-gp72m dbus-daemon[2061]: [session uid=1000 pid=2059] Successfully activated service 'org.freedesktop.Flatpak'
nov 07 14:07:28 msi-gp72m dbus-daemon[2061]: [session uid=1000 pid=2059] Activating service name='org.freedesktop.portal.Documents' requested by ':1.128' (uid=1000 pid=4045 comm="/usr/bin/flatpak run --branch=stable --arch=x86_64")
nov 07 14:07:28 msi-gp72m dbus-daemon[2061]: [session uid=1000 pid=2059] Successfully activated service 'org.freedesktop.portal.Documents'
nov 07 14:07:28 msi-gp72m org.freedesktop.portal.Documents[4087]: fusermount3: failed to access mountpoint /run/user/1000/doc: Permission denied
nov 07 14:07:28 msi-gp72m org.freedesktop.portal.Documents[4080]: error: fuse init failed: Can't mount path /run/user/1000/doc
nov 07 14:07:28 msi-gp72m systemd[1996]: Started app-flatpak-com.github.wwmm.easyeffects-4045.scope.
nov 07 14:07:28 msi-gp72m rtkit-daemon[1726]: Successfully made thread 4104 of process 4096 owned by '1000' RT at priority 20.
nov 07 14:07:28 msi-gp72m rtkit-daemon[1726]: Supervising 7 threads of 4 processes of 1 users.
nov 07 14:07:28 msi-gp72m pipewire[2010]: mod.client-node: detected old client version 4
nov 07 14:07:28 msi-gp72m pipewire[2010]: mod.client-node: detected old client version 4
nov 07 14:07:28 msi-gp72m pipewire[2010]: mod.client-node: detected old client version 4
nov 07 14:07:28 msi-gp72m pipewire[2010]: mod.client-node: detected old client version 4
nov 07 14:07:29 msi-gp72m pipewire[2010]: mod.client-node: detected old client version 4
nov 07 14:07:29 msi-gp72m dbus-daemon[2061]: [session uid=1000 pid=2059] Activating service name='org.freedesktop.portal.IBus' requested by ':1.134' (uid=1000 pid=4092 comm="xdg-dbus-proxy --args=53")
nov 07 14:07:29 msi-gp72m org.freedesktop.portal.IBus[4116]: Not connected to the ibus bus
nov 07 14:07:29 msi-gp72m dbus-daemon[2061]: [session uid=1000 pid=2059] Activated service 'org.freedesktop.portal.IBus' failed: Process org.freedesktop.portal.IBus exited with status 1
nov 07 14:07:35 msi-gp72m dbus-daemon[2061]: [session uid=1000 pid=2059] Activating service name='org.freedesktop.systemd1' requested by ':1.135' (uid=1000 pid=2372 comm="cosmic-app-list")
nov 07 14:07:35 msi-gp72m dbus-daemon[2061]: [session uid=1000 pid=2059] Activated service 'org.freedesktop.systemd1' failed: Process org.freedesktop.systemd1 exited with status 1
nov 07 14:07:36 msi-gp72m dbus-daemon[2061]: [session uid=1000 pid=2059] Activating service name='org.freedesktop.portal.Documents' requested by ':1.137' (uid=1000 pid=4122 comm="/usr/bin/flatpak run --branch=beta --arch=x86_64 -")
nov 07 14:07:36 msi-gp72m dbus-daemon[2061]: [session uid=1000 pid=2059] Successfully activated service 'org.freedesktop.portal.Documents'
nov 07 14:07:36 msi-gp72m org.freedesktop.portal.Documents[4144]: fusermount3: failed to access mountpoint /run/user/1000/doc: Permission denied
nov 07 14:07:36 msi-gp72m org.freedesktop.portal.Documents[4137]: error: fuse init failed: Can't mount path /run/user/1000/doc
nov 07 14:07:36 msi-gp72m systemd[1996]: Started app-flatpak-org.mozilla.firefox-4122.scope.
nov 07 14:07:37 msi-gp72m dbus-daemon[2061]: [session uid=1000 pid=2059] Activating service name='org.freedesktop.portal.IBus' requested by ':1.141' (uid=1000 pid=4149 comm="xdg-dbus-proxy --args=46")
nov 07 14:07:37 msi-gp72m org.freedesktop.portal.IBus[4225]: Not connected to the ibus bus
nov 07 14:07:37 msi-gp72m dbus-daemon[2061]: [session uid=1000 pid=2059] Activated service 'org.freedesktop.portal.IBus' failed: Process org.freedesktop.portal.IBus exited with status 1
nov 07 14:08:00 msi-gp72m chronyd[1243]: Selected source 15.237.97.214 (paris.time.system76.com)
nov 07 14:11:35 msi-gp72m geoclue[2857]: Failed to query location: Query location SOUP error: Not Found
nov 07 14:11:50 msi-gp72m systemd[1996]: Starting launchpadlib-cache-clean.service - Clean up old files in the Launchpadlib cache...
nov 07 14:11:50 msi-gp72m systemd[1996]: Finished launchpadlib-cache-clean.service - Clean up old files in the Launchpadlib cache.
nov 07 14:12:17 msi-gp72m PackageKit[3316]: daemon quit
nov 07 14:12:17 msi-gp72m systemd[1]: packagekit.service: Deactivated successfully.
nov 07 14:15:01 msi-gp72m CRON[5212]: pam_unix(cron:session): session opened for user root(uid=0) by root(uid=0)
nov 07 14:15:01 msi-gp72m CRON[5213]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
nov 07 14:15:01 msi-gp72m CRON[5212]: pam_unix(cron:session): session closed for user root
nov 07 14:16:41 msi-gp72m geoclue[2857]: Failed to query location: Query location SOUP error: Not Found
nov 07 14:16:43 msi-gp72m geoclue[2857]: Failed to query location: Query location SOUP error: Not Found
nov 07 14:17:01 msi-gp72m CRON[5259]: pam_unix(cron:session): session opened for user root(uid=0) by root(uid=0)
nov 07 14:17:01 msi-gp72m CRON[5260]: (root) CMD (cd / && run-parts --report /etc/cron.hourly)
nov 07 14:17:01 msi-gp72m CRON[5259]: pam_unix(cron:session): session closed for user root
nov 07 14:19:20 msi-gp72m dbus-daemon[2061]: [session uid=1000 pid=2059] Activating service name='org.freedesktop.portal.Documents' requested by ':1.34' (uid=1000 pid=2709 comm="/usr/libexec/xdg-desktop-portal")
nov 07 14:19:20 msi-gp72m dbus-daemon[2061]: [session uid=1000 pid=2059] Successfully activated service 'org.freedesktop.portal.Documents'
nov 07 14:19:20 msi-gp72m org.freedesktop.portal.Documents[5445]: fusermount3: failed to access mountpoint /run/user/1000/doc: Permission denied
nov 07 14:19:20 msi-gp72m org.freedesktop.portal.Documents[5438]: error: fuse init failed: Can't mount path /run/user/1000/doc
nov 07 14:19:22 msi-gp72m dbus-daemon[2061]: [session uid=1000 pid=2059] Activating service name='org.freedesktop.portal.Documents' requested by ':1.34' (uid=1000 pid=2709 comm="/usr/libexec/xdg-desktop-portal")
nov 07 14:19:22 msi-gp72m dbus-daemon[2061]: [session uid=1000 pid=2059] Successfully activated service 'org.freedesktop.portal.Documents'
nov 07 14:19:22 msi-gp72m org.freedesktop.portal.Documents[5477]: fusermount3: failed to access mountpoint /run/user/1000/doc: Permission denied
nov 07 14:19:22 msi-gp72m org.freedesktop.portal.Documents[5468]: error: fuse init failed: Can't mount path /run/user/1000/doc
nov 07 14:19:22 msi-gp72m dbus-daemon[2061]: [session uid=1000 pid=2059] Activating service name='org.freedesktop.portal.Documents' requested by ':1.34' (uid=1000 pid=2709 comm="/usr/libexec/xdg-desktop-portal")
nov 07 14:19:22 msi-gp72m dbus-daemon[2061]: [session uid=1000 pid=2059] Successfully activated service 'org.freedesktop.portal.Documents'
nov 07 14:19:22 msi-gp72m org.freedesktop.portal.Documents[5489]: fusermount3: failed to access mountpoint /run/user/1000/doc: Permission denied
nov 07 14:19:22 msi-gp72m org.freedesktop.portal.Documents[5479]: error: fuse init failed: Can't mount path /run/user/1000/doc
nov 07 14:19:22 msi-gp72m xdg-desktop-por[2709]: Failed to register file:///home/gabriele/Videos/Registrazioni/2024-11-06%2023-45-58.mkv: GDBus.Error:org.freedesktop.DBus.Error.NoReply: Message recipient disconnected from message bus without replying

There's also this weird thing:

gabriele@msi-gp72m:~$ sudo journalctl -e | grep xdg-desktop-portal-cosmic
[sudo] password for gabriele:
nov 07 14:06:16 msi-gp72m cosmic-session[2062]: starting process ' WAYLAND_DISPLAY=wayland-1 DISPLAY=:0 XDG_SESSION_TYPE=wayland WAYLAND_SOCKET=77 PORTAL_WAYLAND_SOCKET=59 /usr/libexec/xdg-desktop-portal-cosmic '
nov 07 14:06:16 msi-gp72m cosmic-session[2062]: Failed to spawn scope for /usr/libexec/xdg-desktop-portal-cosmic. Creating transient unit failed with org.freedesktop.DBus.Error.Spawn.ChildExited: Process org.freedesktop.systemd1 exited with status 1

@joshuamegnauth54
Copy link
Contributor Author

@gabriele2000 I'm not sure what's causing the permission errors. 🤔 The rest of the portal works, right? Like screenshots?

You should open an issue with the same log in case someone else knows, but I'll continue looking into it as well.

@gabriele2000
Copy link

@gabriele2000 I'm not sure what's causing the permission errors. 🤔 The rest of the portal works, right? Like screenshots?

You should open an issue with the same log in case someone else knows, but I'll continue looking into it as well.

I hope the upgrade from 22.04 to 24.04 didn't mess something up too bad, because now the cursor in some flatpak apps is gigantic and there's this weird error.
Anyway, one way to reproduce it is to use firefox (flatpak version) and try to upload a random file using cosmic-files (the picker is cosmic-files now)

@gabriele2000
Copy link

@gabriele2000 I'm not sure what's causing the permission errors. 🤔 The rest of the portal works, right? Like screenshots?

You should open an issue with the same log in case someone else knows, but I'll continue looking into it as well.

I fixed cosmic-greeter during a spark in my imagination, it all started because I was tinkering with dbus and it was the issue, so cosmic-greeter was broken and since it started crucial services (and I was using emptty to log-in, because cosmic-greeter was panicking because of broken configuration that I fixed now) a lot was going on.
Now I can finally give you meaningful logs!

running flatpak run org.mozilla.flatpak and trying to open the file picker gives me this:
(org.mozilla.firefox:2): Gdk-WARNING **: 01:15:37.490: Server is missing xdg_foreign support

now let's see what journalctl says:

nov 29 01:19:57 msi-gp72m cosmic-session[2069]: [2024-11-29T00:19:57Z WARN  sctk_adwaita::buttons] Ignoring unknown button type:
nov 29 01:19:58 msi-gp72m cosmic-session[2069]: thread 'main' panicked at /build/xdg-desktop-portal-cosmic-QXp3YQ/xdg-desktop-portal-cosmic-0.1.0pop1~1732139609~24.04~29f0c05/vendor/iced_widget/src/container.rs:396:49:
nov 29 01:19:58 msi-gp72m cosmic-session[2069]: called `Option::unwrap()` on a `None` value
nov 29 01:19:58 msi-gp72m cosmic-session[2069]: note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
nov 29 01:19:58 msi-gp72m xdg-desktop-por[2369]: Backend call failed: Remote peer disconnected
nov 29 01:19:58 msi-gp72m cosmic-session[2069]: process ' DISPLAY=:1 WAYLAND_DISPLAY=wayland-1 XDG_SESSION_TYPE=wayland WAYLAND_SOCKET=81 PORTAL_WAYLAND_SOCKET=83 /usr/libexec/xdg-desktop-portal-cosmic ' failed with code 101
nov 29 01:19:58 msi-gp72m cosmic-session[2069]: /usr/libexec/xdg-desktop-portal-cosmic exited with error 101
nov 29 01:19:58 msi-gp72m cosmic-session[2069]: draining stdin receiver before restarting process
nov 29 01:19:58 msi-gp72m cosmic-session[2069]: sleeping for 16ms before restarting process /usr/libexec/xdg-desktop-portal-cosmic (restart 4)
nov 29 01:19:58 msi-gp72m cosmic-session[2069]: sending 2 fds
nov 29 01:19:58 msi-gp72m cosmic-session[2069]: restarted process ' DISPLAY=:1 WAYLAND_DISPLAY=wayland-1 XDG_SESSION_TYPE=wayland WAYLAND_SOCKET=83 PORTAL_WAYLAND_SOCKET=85 /usr/libexec/xdg-desktop-portal-cosmic ', now at 5 >
nov 29 01:19:58 msi-gp72m systemd[2053]: Started xdg-desktop-portal-cosmic.scope.
nov 29 01:19:58 msi-gp72m cosmic-session[2069]: [2024-11-29T00:19:58Z WARN  wgpu_hal::gles::adapter] Detected skylake derivative running on mesa i915. Clears to srgb textures will use manual shader clears.
nov 29 01:19:58 msi-gp72m cosmic-session[2069]: [2024-11-29T00:19:58Z WARN  wgpu_hal::gles::egl] Re-initializing Gles context due to Wayland window
nov 29 01:20:00 msi-gp72m cosmic-comp[2117]: Client bug: Unable to re-configure repositioned popup.

That's a lot... I shall look up what is this "error 101"

@joshuamegnauth54
Copy link
Contributor Author

@gabriele2000 I have good news and bad news. The bad news is that I can't replicate that panic, but I remember seeing an issue somewhere where COSMIC Session failed to launch the portal (I can't find it though!).

The good news is that someone is experiencing a similar issue: pop-os/libcosmic#731

I based my implementation on the official specs as well as code from
GNOME, KDE, Xapp (Xfce), and Pantheon (elementary). KDE's imminently
readable codebase served as this implementation's primary inspiration.

Autostart is deprecated but seemingly still used, so this implementation
will still support it for compatibility.

The Background portal depends on a working Access portal as
`xdg-desktop-portal` calls it to show the initial warning.

References:
* https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.impl.portal.Background.html
* https://gitlab.gnome.org/GNOME/xdg-desktop-portal-gnome/-/blob/main/src/background.c
* https://invent.kde.org/plasma/xdg-desktop-portal-kde/-/blob/master/src/background.cpp
* https://github.com/linuxmint/xdg-desktop-portal-xapp/blob/f1c24244f90571209c56b7f45802b70e80da4922/src/background.c
* https://github.com/elementary/portals/blob/d868cfa854c731e0f37615e225d5db07cc3f4604/src/Background/Portal.vala
* flatpak/xdg-desktop-portal#1188
@joshuamegnauth54 joshuamegnauth54 force-pushed the feat-background-autorun-portal branch from 77b1577 to f986a4d Compare December 3, 2024 06:14
(This commit also holds any changes from rebases)
@joshuamegnauth54 joshuamegnauth54 force-pushed the feat-background-autorun-portal branch from f986a4d to b4ef89f Compare December 3, 2024 06:15
@joshuamegnauth54 joshuamegnauth54 changed the title Background portal Background portal (Rebased 12-03-2024) Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Background Portal unsupported
4 participants