Skip to content

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
@joshuamegnauth54 joshuamegnauth54 force-pushed the feat-background-autorun-portal branch from b4ef89f to 535c254 Compare May 22, 2025 01:32
@joshuamegnauth54 joshuamegnauth54 changed the title Background portal (Rebased 12-03-2024) Background portal (Rebased 05-21-2025) May 22, 2025
@joshuamegnauth54
Copy link
Contributor Author

@ids1024 Sorry for the tag! I rebased this on main and tested it again with a few Flatpaks to ensure it still works.

To test:

  1. Install a Flatpak'd program that requests background permissions, like Easyeffects.
  2. Let the program request background permissions. For Easyeffects, opening the settings tab is enough.
  3. WITHOUT the portal, the program raises an error. With the portal (this patch), the permissions are correctly applied without an error.
  4. You can double check with the command flatpak permissions and reset the permissions per app with flatpak permission-reset APP to test it again (yay).

Another useful command is /usr/libexec/xdg-desktop-portal -v to check that the COSMIC portal is providing the Background backend.

Let me know if there is anything else I should do. Thanks for your time! 😁

};

AppState {
app_id: info.app_id,
Copy link
Member

Choose a reason for hiding this comment

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

I suppose we don't do anything to verify the value the client gives in https://wayland.app/protocols/xdg-shell#xdg_toplevel:request:set_app_id, even for sandboxed apps. I wonder if that could cause issues.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I do anything about this? 🤔 I'm not sure how to verify if the ids are real.

Comment on lines 341 to 345
#[zvariant(signature = "{sv}")]
struct AppState {
app_id: String,
status: AppStatus,
}
Copy link
Member

Choose a reason for hiding this comment

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

Presumably instead of HashSet<AppState>, it could work to just use HashMap<String, AppStatus>?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll try it. I remember I had some problems figuring out the types, but HashMap<String, AppStatus> should work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I struggled with the types a bit but I finally got it to work again! 😸 I'll squash the last two temp commits pending any additional feedback.

@joshuamegnauth54
Copy link
Contributor Author

I'll squash that commit after testing tonight or tomorrow. Sorry for taking so long!

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 8 times, most recently from b7c92a7 to 1571858 Compare May 28, 2025 06:15
Squashed:
* Rebase on the latest zbus
* Fix types for get_app_state to not return an error nor a nested dict
* Fix get_app_state so that apps default to running in the background
  unless they have open toplevels
@joshuamegnauth54 joshuamegnauth54 force-pushed the feat-background-autorun-portal branch from 1571858 to 967b091 Compare May 28, 2025 06:20
@ids1024
Copy link
Member

ids1024 commented Jun 9, 2025

I've taken more of a look at how xdg-desktop-portal-gnome implements this.

It uses org.gnome.Shell.Introspect to get information about this (this protocol is restricted to the portal client, but can be tested by disabling that restriction with global.context.unsafe_mode = true in Looking Glass). This has a property called sandboxed_app_id.

pop-os/cosmic-protocols#59 has a prototype for how we could expose that on cosmic-comp.

But we can probably merge this, and update it to use that later.

@joshuamegnauth54
Copy link
Contributor Author

Yep! I saw org.gnome.Shell.Introspect when I originally attempted this patch. My first solution used a flawed method of getting running programs, but Drakulix's review suggested using systemd until cosmic-comp exposes something that can be used directly.

@ids1024
Copy link
Member

ids1024 commented Jun 11, 2025

The implementation here generally looks good.

examples/background.rs isn't compiling now, due to changes in Iced since the earlier version of this PR.

Edit: Easy enough to fix, and it looks like I can push to the branch here.

ids1024 added 2 commits June 10, 2025 19:02
`xdg-desktop-portal` expects this. It's easy enough to serialize this
way.
@ids1024
Copy link
Member

ids1024 commented Jun 11, 2025

It seems we do need to serialize AppStatus as v, but that is still cleaner than dealing with a HashSet here. Added a commit.

RunningApplicationsChanged seems to be spammed a bit much. I seem to see that a bunch of times, whenever the focused window changes.

@joshuamegnauth54
Copy link
Contributor Author

I'm not sure if RunningApplicationsChanged triggered so much is intentional. The docs state that it should be emitted "when applications change their state" but tapers it with when it's "worth" calling GetAppState again.

It seems like GNOME only triggers it for apps that changed its background state (link is to GNOME code).

I can update this patch to do so too by caching the old state then checking if an app is newly running in the background.

@ids1024
Copy link
Member

ids1024 commented Jun 12, 2025

It presumably shouldn't be sent every time the focused window changes. But should when an application that is running in the background opens a window, or closes one.

Having it sent in response to new_toplevel or toplevel_closed, but not update_toplevel could be right, though technically https://wayland.app/protocols/xdg-shell#xdg_toplevel:request:set_app_id could be sent at any time.

It's also a bit awkward how toplevel_signal() has a mutex and condvar, but then the background portal just uses a thread to convert from that to a subscription::Event::BackgroundToplevels? This isn't something we could use for anything other than the background portal either, since the *updated = false would break things if we tried to do this in two places and expected both to be signaled. I guess if the WaylandHelper needs to trigger an event, it could have some method returning a Subscription.

@joshuamegnauth54
Copy link
Contributor Author

joshuamegnauth54 commented Jun 13, 2025

I replaced the condvar with a subscription but I'll have to test it tomorrow. I pushed the commit anyway in case you want to comment on the code. 😺

I'm not sure what to do about the new_toplevel, toplevel_closed thingy but I'll look into that later as well.

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