Bug 486506 - Firefox (Flatpak) does not inhibit power management when playing videos
Summary: Firefox (Flatpak) does not inhibit power management when playing videos
Status: RESOLVED FIXED
Alias: None
Product: Powerdevil
Classification: Plasma
Component: general (show other bugs)
Version: 5.27.11
Platform: Kubuntu Linux
: HI normal
Target Milestone: ---
Assignee: Plasma Bugs List
URL:
Keywords:
: 433452 463017 485376 489491 489510 491129 (view as bug list)
Depends on:
Blocks:
 
Reported: 2024-05-03 13:17 UTC by Ahmed
Modified: 2024-08-01 18:25 UTC (History)
16 users (show)

See Also:
Latest Commit:
Version Fixed In: 6.1.4
Sentry Crash Report:


Attachments
The message that appears under "Battery and Brightness" (24.93 KB, image/png)
2024-05-03 13:17 UTC, Ahmed
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Ahmed 2024-05-03 13:17:59 UTC
Created attachment 169137 [details]
The message that appears under "Battery and Brightness"

SUMMARY
Firefox (Flatpak) does not inhibit power management when playing videos, despite the message that appears under "Battery and Brightness" stating that Firefox is currently blocking sleep and screen locking (please check attachments).

STEPS TO REPRODUCE
1. Open Firefox (Flatpak).
2. Play a video.

OBSERVED RESULT
Firefox (Flatpak) does not inhibit power management when playing videos

EXPECTED RESULT
Firefox (Flatpak) to inhibit power management when playing videos

SOFTWARE/OS VERSIONS
Linux/KDE Plasma:  Kubuntu 24.04
KDE Plasma Version: 5.27.11
KDE Frameworks Version:  5.115.0
Qt Version: 5.15.13
Comment 1 Natalie Clarius 2024-05-03 13:44:59 UTC
What are the ways you notice that it is not blocked?
Comment 2 Ahmed 2024-05-03 14:29:03 UTC
The bug is similar to this bug -> (In reply to Natalie Clarius from comment #1)
> What are the ways you notice that it is not blocked?

First, the screen is locked after 5 minutes, and I disabled "Screen Locking", then the screen was dimmed after 5 minutes, and it was turned off after 10 minutes, so I knew that power management is not inhibited.

I was playing an audio file using Elisa earlier today, and my machine did not go to sleep, so I did not suspect anything wrong until I used Firefox (Flatpak) and I discovered this issue.

I installed Kubuntu 24.04 yesterday (fresh install, no upgrade).
Comment 3 Ahmed 2024-05-03 14:31:22 UTC
Typo: the screen was locked after 5 minutes.
Comment 4 Justin Zobel 2024-05-04 03:11:23 UTC
I can confirm this on my Steam Deck. I had Netflix playing in Firefox. The Plasma power widget even shows the message saying Firefox is blocking but then 1 minute later my display dims and then turns off.
Comment 5 JLV 2024-05-16 07:35:55 UTC
The same thing happens to me with my 3 Linux systems. When playing a YouTube video, the screensaver does not automatically inhibit and the session closes after 5 minutes. I am also using the flatpak version.
Comment 6 Kai Uwe Broulik 2024-05-16 07:58:59 UTC
Perhaps the sandbox prevents it from working properly like it breaks Plasma Browser Integration, too....
Comment 7 Jan Rathmann 2024-06-03 15:38:39 UTC
I get the same behaviour with the flatpaked version of SameBoy (screen goes black while playing with Gamepad despite SameBoy is listed as "blocking screen locking and suspend" in systray).

However, if I watch videos with Freetube (also Flatpak), suspend inhibition works correct (screen stays on).

My setup:
* Kubuntu 24.04
* self-build Plasma 6.0.90, Frameworks 6.4.0 and Qt 6.7.2
* Wayland session
Comment 8 Nate Graham 2024-07-02 20:12:16 UTC
*** Bug 489491 has been marked as a duplicate of this bug. ***
Comment 9 Szymon Łągiewka 2024-07-02 20:34:41 UTC
*** Bug 489510 has been marked as a duplicate of this bug. ***
Comment 10 Valeriy 2024-07-04 19:24:21 UTC
Hello, everyone. Seems like this is a result of misconfiguration on the flatpak package's maintainers. Add permission to talk with org.freedesktop.ScreenSaver of Session Bus and Firefox should prevent the screen from locking again if you watch videos.
Comment 11 Justin Zobel 2024-07-05 01:05:28 UTC
(In reply to Valeriy from comment #10)
> Hello, everyone. Seems like this is a result of misconfiguration on the
> flatpak package's maintainers. Add permission to talk with
> org.freedesktop.ScreenSaver of Session Bus and Firefox should prevent the
> screen from locking again if you watch videos.

Have you tested this with a local build or confirmed this from Mozilla on their Bugzilla tracker?
Comment 12 Valeriy 2024-07-05 08:14:18 UTC
(In reply to Justin Zobel from comment #11)
> (In reply to Valeriy from comment #10)
> > Hello, everyone. Seems like this is a result of misconfiguration on the
> > flatpak package's maintainers. Add permission to talk with
> > org.freedesktop.ScreenSaver of Session Bus and Firefox should prevent the
> > screen from locking again if you watch videos.
> 
> Have you tested this with a local build or confirmed this from Mozilla on
> their Bugzilla tracker?

I've only tested this locally on my machine.
Comment 13 Justin Zobel 2024-07-05 09:35:17 UTC
(In reply to Valeriy from comment #12)
> (In reply to Justin Zobel from comment #11)
> > (In reply to Valeriy from comment #10)
> > > Hello, everyone. Seems like this is a result of misconfiguration on the
> > > flatpak package's maintainers. Add permission to talk with
> > > org.freedesktop.ScreenSaver of Session Bus and Firefox should prevent the
> > > screen from locking again if you watch videos.
> > 
> > Have you tested this with a local build or confirmed this from Mozilla on
> > their Bugzilla tracker?
> 
> I've only tested this locally on my machine.

Well if it works there I'd suggest reporting to Mozilla so they can integrate it into their Flatpak build.
Comment 14 Jakob Petsovits 2024-07-05 11:24:07 UTC
(In reply to Justin Zobel from comment #13)
> (In reply to Valeriy from comment #12)
> > (In reply to Justin Zobel from comment #11)
> > > (In reply to Valeriy from comment #10)
> > > > Hello, everyone. Seems like this is a result of misconfiguration on the
> > > > flatpak package's maintainers. Add permission to talk with
> > > > org.freedesktop.ScreenSaver of Session Bus and Firefox should prevent the
> > > > screen from locking again if you watch videos.
> > > 
> > > Have you tested this with a local build or confirmed this from Mozilla on
> > > their Bugzilla tracker?
> > 
> > I've only tested this locally on my machine.
> 
> Well if it works there I'd suggest reporting to Mozilla so they can
> integrate it into their Flatpak build.

In a Flatpak environment, they should probably use the `org.freedesktop.portal.Inhibit` interface [1] rather than `org.freedesktop.Inhibit`. KDE's portal implementation exists [2], so in theory this would allow the most cross-platform implementation as `org.freedesktop.Inhibit` is now considered legacy for non-sandboxed apps (those should use the systemd inhibit interface [3] instead). But I agree with Justin that Firefox developers should have a look at why their Flatpak build isn't using the portal API.

[1] https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Inhibit.html
[2] https://invent.kde.org/plasma/xdg-desktop-portal-kde/-/blob/master/src/inhibit.cpp
[3] https://www.freedesktop.org/software/systemd/man/latest/org.freedesktop.login1.html
Comment 15 Nate Graham 2024-07-22 20:48:55 UTC
Someone report or fix upstream, please.
Comment 16 Jakob Petsovits 2024-07-23 09:02:43 UTC
https://bugs.kde.org/show_bug.cgi?id=472541#c8 had an interesting observation, in that KDE implements Wayland inhibition locks correctly but some apps or inhibit helpers don't associate them to focused Wayland surfaces and thus the lock is not respected by the compositor. https://codeberg.org/river/river/issues/1079#issuecomment-2013817 has another comment about this. https://bugzilla.mozilla.org/show_bug.cgi?id=1877022 was a Firefox issue for that problem.

This leads to the actual source code in https://hg.mozilla.org/mozilla-central/file/tip/widget/gtk/WakeLockListener.cpp which shows that Firefox does implement support for the xdg portal D-Bus API, however only as a fallback after the old-school `org.freedesktop.Inhibit` D-Bus API has been tried. The aforementioned Wayland API is yet different and only a last-resort fallback, so probably not further relevant for the purpose of this bug.

In theory, Firefox should be trying the various D-Bus interfaces in order, and switch to the next candidate if the previous ones don't work. We should trace D-Bus calls via dbus-monitor before punting responsibility onto Firefox, especially if a "blocking sleep and screen locking" message appears in the Battery applet.
Comment 17 Nate Graham 2024-07-23 16:26:30 UTC
> especially if a "blocking sleep and screen locking" message appears in the Battery applet.
Indeed, that happens. So the message appears but isn't accurate.
Comment 18 Jakob Petsovits 2024-07-29 15:15:30 UTC
I ran dbus-monitor to see what's going on. It makes sense now and I've got a fix. The disconnect is that xdg-desktop-portals-kde receives an `Inhibit` request with flags 8 from Firefox, which according to the linked docs means inhibiting "Idle":

https://docs.flatpak.org/en/latest/portal-api-reference.html#gdbus-org.freedesktop.portal.Inhibit

Following that, xdg-desktop-portals-kde calls PowerDevil's `AddInhibition` with a "policies" argument of value 1, a.k.a. "InterruptSession". PowerDevil receives and registers it as requested. You'd think that this would keep the locker from firing? No, in fact, what "InterruptSession" does is to prevent sleep, not idle. The XDG portals API has a different flag for that, called "Suspend" with value 4, not 8.

Hence the fix is to ask PowerDevil for the correct policy, confusingly called "ChangeScreenSettings" with value 4, not 1. We're already using this for logind "idle" inhibitors, so I'm pretty confident that it's the right pick also for portals "Idle".

This also explains why the applet was showing an indicator: PowerDevil was in fact inhibiting, except not against screen locking but merely against system sleep.

Here's the relevant dbus-monitor output for reference, from Firefox registering the inhibition to PowerDevil receiving it:

method call time=1722263501.828060 sender=:1.262 -> destination=:1.6 serial=263 path=/org/freedesktop/portal/desktop; interface=org.freedesktop.portal.Inhibit; member=Inhibit
   string "org.mozilla.firefox"
   uint32 8
   array [
      dict entry(
         string "reason"
         variant             string "video-playing"
      )
   ]
method call time=1722263501.828599 sender=:1.6 -> destination=org.freedesktop.DBus serial=9496 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch
   string "type='signal',sender='org.freedesktop.impl.portal.desktop.kde',interface='org.freedesktop.impl.portal.Request',path='/org/freedesktop/portal/desktop/request/1_262/t/3738610708'"
method return time=1722263501.828618 sender=org.freedesktop.DBus -> destination=:1.6 serial=4294967295 reply_serial=9496
method call time=1722263501.828625 sender=:1.6 -> destination=org.freedesktop.DBus serial=9497 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=StartServiceByName
   string "org.freedesktop.impl.portal.desktop.kde"
   uint32 0
method return time=1722263501.828640 sender=org.freedesktop.DBus -> destination=:1.6 serial=4294967295 reply_serial=9497
   uint32 2
method call time=1722263501.828871 sender=:1.6 -> destination=org.freedesktop.DBus serial=9498 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=GetNameOwner
   string "org.freedesktop.impl.portal.desktop.kde"
method return time=1722263501.828888 sender=org.freedesktop.DBus -> destination=:1.6 serial=4294967295 reply_serial=9498
   string ":1.31"
method return time=1722263501.829171 sender=:1.6 -> destination=:1.262 serial=9499 reply_serial=263
   object path "/org/freedesktop/portal/desktop/request/1_262/t/3738610708"
method call time=1722263501.829311 sender=:1.6 -> destination=:1.9 serial=9500 path=/org/freedesktop/impl/portal/PermissionStore; interface=org.freedesktop.impl.portal.PermissionStore; member=Lookup
   string "inhibit"
   string "inhibit"
error time=1722263501.829910 sender=:1.9 -> destination=:1.6 error_name=org.freedesktop.portal.Error.NotFound reply_serial=9500
   string "No entry for inhibit"
method call time=1722263501.830200 sender=:1.6 -> destination=:1.31 serial=9501 path=/org/freedesktop/portal/desktop; interface=org.freedesktop.impl.portal.Inhibit; member=Inhibit
   object path "/org/freedesktop/portal/desktop/request/1_262/t/3738610708"
   string "org.mozilla.firefox"
   string "org.mozilla.firefox"
   uint32 8
   array [
      dict entry(
         string "reason"
         variant             string "video-playing"
      )
   ]
method call time=1722263501.830491 sender=:1.31 -> destination=org.kde.Solid.PowerManagement serial=13663 path=/org/kde/Solid/PowerManagement/PolicyAgent; interface=org.kde.Solid.PowerManagement.PolicyAgent; member=AddInhibition
   uint32 1
   string "org.mozilla.firefox"
   string "video-playing"
method return time=1722263501.830515 sender=:1.31 -> destination=:1.6 serial=13664 reply_serial=9501
method call time=1722263501.830703 sender=:1.272 -> destination=org.freedesktop.DBus serial=339 path=/org/freedesktop/DBus; interface=org.freedesktop.DBus; member=AddMatch
   string "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0=':1.31',arg2=''"
method return time=1722263501.830924 sender=:1.272 -> destination=:1.31 serial=340 reply_serial=13663
   uint32 9
signal time=1722263501.830937 sender=:1.6 -> destination=:1.262 serial=9502 path=/org/freedesktop/portal/desktop/request/1_262/t/3738610708; interface=org.freedesktop.portal.Request; member=Response
   uint32 0
   array [
   ]
Comment 19 Bug Janitor Service 2024-07-29 15:45:26 UTC
A possibly relevant merge request was started @ https://invent.kde.org/plasma/xdg-desktop-portal-kde/-/merge_requests/305
Comment 20 Nate Graham 2024-07-29 16:46:17 UTC
Great investigation. It sounds like Bug 418433 is not helping here, and inhibited figuring out what was going wrong.
Comment 21 Jakob Petsovits 2024-07-31 10:36:58 UTC
Git commit 1549c49f41f544cb9dad54de509f318cc5b6dc38 by Jakob Petsovits.
Committed on 30/07/2024 at 11:20.
Pushed by jpetso into branch 'master'.

Inhibit: Forward the correct inhibition flags to PolicyAgent

The Inhibit method ignored the value of the "flags" argument and
always sent InterruptSession as inhibition policy. This prevents
sleep, but does not prevent idle actions such as screen locking
and dimming.

This commit changes the requested inhibition policies to match
the documented values for "flags":

* Portals "Suspend" remains PolicyAgent "InterruptSession".
* Portals "Idle" becomes PolicyAgent "ChangeScreenSettings".
* Portals "Logout" and "User Switch" are not supported at this time
  and are merely logged but otherwise ignored.

PowerDevil, which implements the PolicyAgent API, uses the same
policies also to represent the logind "sleep" and "idle" inhibitors,
so we can trust that they behave accordingly.
Related: bug 472541, bug 335729

M  +12   -2    src/inhibit.cpp

https://invent.kde.org/plasma/xdg-desktop-portal-kde/-/commit/1549c49f41f544cb9dad54de509f318cc5b6dc38
Comment 22 Jakob Petsovits 2024-07-31 10:40:28 UTC
Git commit 8fd86c5b0e6d6797bff850947b07e086544cd617 by Jakob Petsovits.
Committed on 31/07/2024 at 10:37.
Pushed by jpetso into branch 'Plasma/6.1'.

Inhibit: Forward the correct inhibition flags to PolicyAgent

The Inhibit method ignored the value of the "flags" argument and
always sent InterruptSession as inhibition policy. This prevents
sleep, but does not prevent idle actions such as screen locking
and dimming.

This commit changes the requested inhibition policies to match
the documented values for "flags":

* Portals "Suspend" remains PolicyAgent "InterruptSession".
* Portals "Idle" becomes PolicyAgent "ChangeScreenSettings".
* Portals "Logout" and "User Switch" are not supported at this time
  and are merely logged but otherwise ignored.

PowerDevil, which implements the PolicyAgent API, uses the same
policies also to represent the logind "sleep" and "idle" inhibitors,
so we can trust that they behave accordingly.
Related: bug 472541, bug 335729


(cherry picked from commit 1549c49f41f544cb9dad54de509f318cc5b6dc38)

Co-authored-by: Jakob Petsovits <jpetso@petsovits.com>

M  +12   -2    src/inhibit.cpp

https://invent.kde.org/plasma/xdg-desktop-portal-kde/-/commit/8fd86c5b0e6d6797bff850947b07e086544cd617
Comment 23 Nate Graham 2024-07-31 18:16:20 UTC
*** Bug 485376 has been marked as a duplicate of this bug. ***
Comment 24 Nate Graham 2024-07-31 18:16:27 UTC
*** Bug 433452 has been marked as a duplicate of this bug. ***
Comment 25 Nate Graham 2024-07-31 18:16:30 UTC
*** Bug 463017 has been marked as a duplicate of this bug. ***
Comment 26 Nate Graham 2024-08-01 18:25:49 UTC
*** Bug 491129 has been marked as a duplicate of this bug. ***