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

XdpAppInfo refactoring, Part 2 #1369

Merged
merged 11 commits into from
Sep 14, 2024

Conversation

swick
Copy link
Contributor

@swick swick commented Jun 5, 2024

This is a continuation of #1366.

This moves the app info kind specific code to subclasses, the pid mapping stuff to utils and reduces the scope of XdpAppInfo.

@swick swick force-pushed the wip/xdp-app-info-subclasses branch 3 times, most recently from 1c5df96 to aaf69ba Compare June 8, 2024 13:25
@swick swick marked this pull request as ready for review June 8, 2024 13:25
@swick swick force-pushed the wip/xdp-app-info-subclasses branch 2 times, most recently from a034dff to 40f1779 Compare June 18, 2024 15:54
@swick
Copy link
Contributor Author

swick commented Jun 18, 2024

Rebased on top of the changes from Part 1.

@swick swick force-pushed the wip/xdp-app-info-subclasses branch from 40f1779 to 6595192 Compare June 26, 2024 10:24
@swick
Copy link
Contributor Author

swick commented Jun 26, 2024

Rebased and added one more commit.

Comment on lines 33 to 36
gboolean xdp_app_info_flatpak_new (int pid,
int pidfd,
XdpAppInfo **out_app_info,
GError **error);
Copy link
Member

Choose a reason for hiding this comment

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

I think constructors like this should always return the object they're constructing:

XdpAppInfo *
xdp_app_info_flatpak_new (int      pid,
                          int      pidfd,
                          GError **error);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's not possible because this is basically a tri-state. While creating it, errors can occur, in which case no further attempt must be made to create another XdpAppInfo (because that might create a host XdpAppInfo for a flatpak app). It however can also be possible that the app just isn't e.g. a flatpak app and that snap and then host should be tried later.

The API is as good as it gets with those semantics because it follows the "returns FALSE when error is set" convention while also allowing to either create or not create an XdpAppInfo.

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's not possible because this is basically a tri-state. While creating it, errors can occur, in which case no further attempt must be made to create another XdpAppInfo (because that might create a host XdpAppInfo for a flatpak app).

That's what GInitable is for.

The API is as good as it gets with those semantics because it follows the "returns FALSE when error is set"

You can return NULL and set the error. Typical constructors cannot return NULL, but fallible constructors (like anything that implements GInitable) can do that.

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 don't get it. xdp_app_info_flatpak_new either creates a XdpAppInfo GObject in which case there won't be an error, or figures out that the process isn't a flatpak process, or runs into an error while trying to figure out if the process is a flatpak process or not.

GInitiable doesn't help here at all because the GObject initialization always succeeds.

You can return NULL and set the error.

I also can do this here, and that's what the code did before, but then NULL can be returned and it either means there was an error (when error is set) or the process isn't a flatpak process (when error is not set).

Copy link
Collaborator

Choose a reason for hiding this comment

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

how about returning NULL and setting the GError. Then the GError can indicate the proper reason. You annotate that it is nullable (there is no associated doc at the moment it seems). Also about the "no further attempt" the code must ensure of that, to prevent API misuse.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Returning NULL meaning either "not a flatpak process" or "there was an error" is exactly how it worked before. In glib and a lot of other GNOME components there is a convention that the success/error state is returned and the error is just an additional piece of information. This change is so that it follows this convention.

I really don't understand the problem here. Out arguments are common. The error argument itself is such a thing.

Copy link
Member

Choose a reason for hiding this comment

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

I think what I was trying to say is, a common pattern for this kind of situation is to define a custom GError, and use it. Here's an example in Boatswain.

The BS_STREAM_DECK_ERROR_UNRECOGNIZED is defined specifically for the purpose of saying: this device is not a recognized Elgato device. Creating an object will fail (the purpose of GInitable is for failable constructors after all), and we use the extra bit of information - the error itself - to decide how to proceed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I'm now using a new XDG_DESKTOP_PORTAL_ERROR_WRONG_APP_KIND error to signal that the process isn't the of the app kind which was attempted to be created.

@jsparber
Copy link
Contributor

jsparber commented Jul 3, 2024

Wouldn't it make sense to validate_desktop_file_id() from the DynamicLauncher to be part of xdp_app_info_validate_dynamic_launcher()?

But maybe it could also be a separate vfunc so it can be reused by the notification spec v2

@swick
Copy link
Contributor Author

swick commented Jul 3, 2024

Wanted to leave that for a future cleanup but I've done that now (last two commits).

@swick
Copy link
Contributor Author

swick commented Aug 6, 2024

Removed the last few commits and moved them to a part 3 MR. The commits which are left should not be split any further because that might leave the code in a suboptimal state.

@swick swick force-pushed the wip/xdp-app-info-subclasses branch 3 times, most recently from bfe2e55 to 1c9bf69 Compare September 12, 2024 22:57
This implements XdpAppInfo subclasses for host, tests, flatpak and snap.
They are unused for now and will get instantiated by a later commit.
They are initialized by xdp_app_info_initialize when the object gets
created and will stay valid until it is disposed.

A later commit will start using them to implement existing functions.
The pidns is derived from the pidfd of a process from within the same
pidns as the app.
Now that pid mapping is done by getting the pidns from the XdpAppInfo
and then using xdp-utils to do the actual mappings, we can remove the
mapping functionality from XdpAppInfo.
There is no remaining user of this function so we can make it private
and remove it from the header.
Implement existing functions using the new private fields of XdpAppInfo
and the new vfuncs of the class. Also instantiate the subclasses.

This allows us to remove the app info kind and the kind specific fields
from XdpAppInfo and delegates all of that to the subclasses.
@GeorgesStavracas GeorgesStavracas added this pull request to the merge queue Sep 14, 2024
@GeorgesStavracas GeorgesStavracas added this to the 1.20 milestone Sep 14, 2024
Merged via the queue into flatpak:main with commit f6ae3a8 Sep 14, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Triaged
Development

Successfully merging this pull request may close these issues.

5 participants