-
Notifications
You must be signed in to change notification settings - Fork 8
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
clubhouse: Request background permission #1218
Open
danigm
wants to merge
1
commit into
master
Choose a base branch
from
request-background
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Copyright (C) 2020 Endless OS Foundation LLC. | ||
# | ||
# This program is free software; you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation; either version 2 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License along | ||
# with this program; if not, write to the Free Software Foundation, Inc., | ||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
# | ||
|
||
import logging | ||
|
||
from gi.repository import Gio, GLib | ||
|
||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
class Portal: | ||
_proxy = None | ||
_response_signal_id = None | ||
|
||
@classmethod | ||
def _get_proxy(klass): | ||
if klass._proxy is None: | ||
klass._proxy = Gio.DBusProxy.new_for_bus_sync( | ||
Gio.BusType.SESSION, | ||
0, | ||
None, | ||
'org.freedesktop.portal.Desktop', | ||
'/org/freedesktop/portal/desktop', | ||
'org.freedesktop.portal.Background', | ||
None | ||
) | ||
return klass._proxy | ||
|
||
@classmethod | ||
def request_background(klass, message='', callback=None): | ||
handle_token = 'clubhouse_req_hack' | ||
try: | ||
klass._connect_response(handle_token, callback) | ||
proxy = klass._get_proxy() | ||
options = { | ||
'handle_token': GLib.Variant('s', handle_token), | ||
} | ||
if message: | ||
options['reason'] = GLib.Variant('s', message) | ||
proxy.RequestBackground('(sa{sv})', '', options) | ||
except GLib.Error as err: | ||
_logger.warning('Error requesting background permission to portal: %s', err) | ||
|
||
@classmethod | ||
def _connect_response(klass, token, callback): | ||
proxy = klass._get_proxy() | ||
connection = proxy.get_connection() | ||
|
||
sender = connection.get_unique_name()[1:].replace('.', '_') | ||
handle = f'/org/freedesktop/portal/desktop/request/{sender}/{token}' | ||
|
||
if klass._response_signal_id: | ||
klass._disconnect_response() | ||
|
||
def _response_received(conn, sender_name, object_path, interface_name, | ||
signal_name, parameters, user_data): | ||
if parameters: | ||
_id, params = parameters.unpack() | ||
if callback: | ||
callback(params['background']) | ||
|
||
klass._response_signal_id = Gio.DBusConnection.signal_subscribe( | ||
connection, | ||
'org.freedesktop.portal.Desktop', | ||
'org.freedesktop.portal.Request', | ||
'Response', | ||
handle, | ||
None, | ||
Gio.DBusSignalFlags.NO_MATCH_RULE, | ||
_response_received, | ||
None, | ||
) | ||
|
||
@classmethod | ||
def _disconnect_response(klass): | ||
proxy = klass._get_proxy() | ||
connection = proxy.get_connection() | ||
Gio.DBusConnection.signal_unsubscribe(connection, | ||
klass._response_signal_id) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is another error case here which might be worth handling by watching the returned Request object. If the user denies the request to run in the background, the portal will never ask again. The user needs to do
flatpak permission-reset com.hack_computer.Clubhouse
or poke around in GNOME Control Center to fix it. (Or there might be a way to reset your permissions inside a flatpak? I can't say I actually looked very hard when I did it).Here's some code where I did that in GNOME Break Timer:
https://gitlab.gnome.org/GNOME/gnome-break-timer/-/blob/master/src/settings/MainWindow.vala#L278-311
https://gitlab.gnome.org/GNOME/gnome-break-timer/-/blob/master/src/settings/BreakManager.vala#L117-210
https://gitlab.gnome.org/GNOME/gnome-break-timer/-/blob/master/src/settings/MainWindow.vala#L278-311
For Clubhouse this might not be too much of a problem since it's just asking for the Background permission to remove pesky notifications, although it is rather unfortunate to have only one chance to do it right when we aren't handling the error. (It makes it really fiddly to test, for one thing).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this long and detailed description of the service, I'll take a second look to try to improve the integration. I've been doing some testing with a fedora 33 virtual machine and it looks like that just with the request, the inactivity dialog dissapears, but maybe that's just a implementation issue and this can fail in other distributions, so I'll try to implement the whole flow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I expect what might be happening there is it gets denied permission, rather than simply not granted. (Which feels like a bug in the portal, come to think of it. I reported it over here: flatpak/xdg-desktop-portal#551).
So then it is technically not allowed to run in the background, although I think what happens from there is undefined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've detected that if the user allows, the app will run in background without any problem, and if the user declines, the app will be stopped when the window disappears, after a few seconds. But there's no notification, if the app request the background permission, the system do that automatically without asking again or notify to the user.