polkit: try harder to make polkit optional on non-Linux

We want the viewer to be usable on Mac/Windows/FreeBSD, even if our
profiler tooling isn't (yet).

Another option could be to do a simplified viewer window for those
platforms, but I think that is more work to maintain than abstracting
some of the stuff better in libsysprof.
This commit is contained in:
Christian Hergert
2019-06-04 14:28:06 -07:00
parent dcd1a9fbcf
commit af2a17daa5
6 changed files with 250 additions and 64 deletions

View File

@ -26,7 +26,7 @@
#include <polkit/polkit.h>
#include "sysprof-helpers.h"
#include "sysprof-backport-autocleanups.h"
#include "sysprof-polkit-private.h"
#include "helpers.h"
#include "ipc-service.h"
@ -526,63 +526,22 @@ sysprof_helpers_perf_event_open (SysprofHelpers *self,
#endif /* __linux__ */
static void
sysprof_helpers_check_authorization_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
sysprof_helpers_authorize_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
PolkitAuthority *authority = (PolkitAuthority *)object;
g_autoptr(PolkitAuthorizationResult) res = NULL;
g_autoptr(GError) error = NULL;
g_autoptr(GTask) task = user_data;
g_autoptr(GError) error = NULL;
g_assert (POLKIT_IS_AUTHORITY (authority));
g_assert (G_IS_ASYNC_RESULT (result));
g_assert (G_IS_TASK (task));
if (!(res = polkit_authority_check_authorization_finish (authority, result, &error)))
if (!_sysprof_polkit_authorize_for_bus_finish (result, &error))
g_task_return_error (task, g_steal_pointer (&error));
else if (!polkit_authorization_result_get_is_authorized (res))
g_task_return_new_error (task,
G_IO_ERROR,
G_IO_ERROR_PROXY_AUTH_FAILED,
"Failed to authorize user credentials");
else
g_task_return_boolean (task, TRUE);
}
static void
sysprof_helpers_get_authority_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
g_autoptr(PolkitAuthority) authority = user_data;
g_autoptr(GError) error = NULL;
g_autoptr(GTask) task = user_data;
PolkitSubject *subject;
GCancellable *cancellable;
g_assert (G_IS_ASYNC_RESULT (result));
g_assert (G_IS_TASK (task));
cancellable = g_task_get_cancellable (task);
subject = g_task_get_task_data (task);
g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
g_assert (POLKIT_IS_SUBJECT (subject));
if (!(authority = polkit_authority_get_finish (result, &error)))
g_task_return_error (task, g_steal_pointer (&error));
else
polkit_authority_check_authorization (authority,
subject,
"org.gnome.sysprof3.profile",
NULL,
POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
cancellable,
sysprof_helpers_check_authorization_cb,
g_steal_pointer (&task));
}
void
sysprof_helpers_authorize_async (SysprofHelpers *self,
GCancellable *cancellable,
@ -592,7 +551,6 @@ sysprof_helpers_authorize_async (SysprofHelpers *self,
g_autoptr(GTask) task = NULL;
g_autoptr(PolkitSubject) subject = NULL;
GDBusConnection *bus;
const gchar *unique_name;
g_return_if_fail (SYSPROF_IS_HELPERS (self));
g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
@ -600,17 +558,24 @@ sysprof_helpers_authorize_async (SysprofHelpers *self,
task = g_task_new (self, cancellable, callback, user_data);
g_task_set_source_tag (task, sysprof_helpers_authorize_async);
if (fail_if_no_proxy (self, task))
return;
if (self->proxy == NULL)
{
/* No D-Bus/Polkit? Just bail early and let the hard failure
* happen sooner.
*/
g_task_return_boolean (task, TRUE);
return;
}
bus = g_dbus_proxy_get_connection (G_DBUS_PROXY (self->proxy));
unique_name = g_dbus_connection_get_unique_name (bus);
subject = polkit_system_bus_name_new (unique_name);
g_task_set_task_data (task, g_steal_pointer (&subject), g_object_unref);
polkit_authority_get_async (cancellable,
sysprof_helpers_get_authority_cb,
g_steal_pointer (&task));
_sysprof_polkit_authorize_for_bus_async (bus,
"org.gnome.sysprof3.profile",
NULL,
TRUE,
cancellable,
sysprof_helpers_authorize_cb,
g_steal_pointer (&task));
}
gboolean