[gnome-shell] screenShield: Asyncify _syncInhibitor()



commit 5791e257e7974750cd576b1952c177ee3b57e231
Author: Sebastian Keller <skeller gnome org>
Date:   Sun Aug 15 02:18:12 2021 +0200

    screenShield: Asyncify _syncInhibitor()
    
    Fixes: https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/4553
    Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1952>

 js/misc/loginManager.js | 26 +++++++++++---------------
 js/ui/screenShield.js   | 28 ++++++++++++----------------
 2 files changed, 23 insertions(+), 31 deletions(-)
---
diff --git a/js/misc/loginManager.js b/js/misc/loginManager.js
index 55e928986c..dec0b120b2 100644
--- a/js/misc/loginManager.js
+++ b/js/misc/loginManager.js
@@ -188,19 +188,14 @@ var LoginManagerSystemd = class {
         this._proxy.SuspendRemote(true);
     }
 
-    async inhibit(reason, callback) {
-        try {
-            const inVariant = new GLib.Variant('(ssss)',
-                ['sleep', 'GNOME Shell', reason, 'delay']);
-            const [outVariant_, fdList] =
-                await this._proxy.call_with_unix_fd_list('Inhibit',
-                    inVariant, 0, -1, null, null);
-            const [fd] = fdList.steal_fds();
-            callback(new Gio.UnixInputStream({ fd }));
-        } catch (e) {
-            logError(e, 'Error getting systemd inhibitor');
-            callback(null);
-        }
+    async inhibit(reason, cancellable) {
+        const inVariant = new GLib.Variant('(ssss)',
+            ['sleep', 'GNOME Shell', reason, 'delay']);
+        const [outVariant_, fdList] =
+            await this._proxy.call_with_unix_fd_list('Inhibit',
+                inVariant, 0, -1, null, cancellable);
+        const [fd] = fdList.steal_fds();
+        return new Gio.UnixInputStream({ fd });
     }
 
     _prepareForSleep(proxy, sender, [aboutToSuspend]) {
@@ -236,8 +231,9 @@ var LoginManagerDummy = class {
         this.emit('prepare-for-sleep', false);
     }
 
-    inhibit(reason, callback) {
-        callback(null);
+    /* eslint-disable-next-line require-await */
+    async inhibit() {
+        return null;
     }
 };
 Signals.addSignalMethods(LoginManagerDummy.prototype);
diff --git a/js/ui/screenShield.js b/js/ui/screenShield.js
index 04353d93b5..87b79b353d 100644
--- a/js/ui/screenShield.js
+++ b/js/ui/screenShield.js
@@ -203,7 +203,7 @@ var ScreenShield = class {
         return this._isModal;
     }
 
-    _syncInhibitor() {
+    async _syncInhibitor() {
         const lockEnabled = this._settings.get_boolean(LOCK_ENABLED_KEY);
         const lockLocked = this._lockSettings.get_boolean(DISABLE_LOCK_KEY);
         const inhibit = !!this._loginSession && this._loginSession.Active &&
@@ -215,22 +215,18 @@ var ScreenShield = class {
 
         this._inhibited = inhibit;
 
+        this._inhibitCancellable?.cancel();
+        this._inhibitCancellable = new Gio.Cancellable();
+
         if (inhibit) {
-            this._loginManager.inhibit(_('GNOME needs to lock the screen'),
-                inhibitor => {
-                    if (inhibitor) {
-                        if (this._inhibitor)
-                            inhibitor.close(null);
-                        else
-                            this._inhibitor = inhibitor;
-                    }
-
-                    // Handle uninhibits that happened after the start
-                    if (!this._inhibited) {
-                        this._inhibitor?.close(null);
-                        this._inhibitor = null;
-                    }
-                });
+            try {
+                this._inhibitor = await this._loginManager.inhibit(
+                    _('GNOME needs to lock the screen'),
+                    this._inhibitCancellable);
+            } catch (e) {
+                if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED))
+                    log('Failed to inhibit suspend: %s'.format(e.message));
+            }
         } else {
             this._inhibitor?.close(null);
             this._inhibitor = null;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]