[gnome-shell] extensionUtils: Add utility for setting the current extension



commit 61eef2fb9bd6f0e3e94ef26a349ecb2e29207156
Author: Evan Welsh <contact evanwelsh com>
Date:   Tue Jul 5 12:09:26 2022 -0400

    extensionUtils: Add utility for setting the current extension
    
    ES modules do not allow exports to be overriden, in anticipation
    of the ESM port add a `setCurrentExtension` utility which will
    throw if used in the shell. This is tested using a conditional
    import of Main.
    
    Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2363>

 js/dbusServices/extensions/extensionPrefsDialog.js |  2 +-
 js/misc/extensionUtils.js                          | 28 +++++++++++++++++++++-
 2 files changed, 28 insertions(+), 2 deletions(-)
---
diff --git a/js/dbusServices/extensions/extensionPrefsDialog.js 
b/js/dbusServices/extensions/extensionPrefsDialog.js
index 4e90da4e70..7155c1a953 100644
--- a/js/dbusServices/extensions/extensionPrefsDialog.js
+++ b/js/dbusServices/extensions/extensionPrefsDialog.js
@@ -18,7 +18,7 @@ var ExtensionPrefsDialog = GObject.registerClass({
             ExtensionUtils.installImporter(extension);
 
             // give extension prefs access to their own extension object
-            ExtensionUtils.getCurrentExtension = () => extension;
+            ExtensionUtils.setCurrentExtension(extension);
 
             const prefsModule = extension.imports.prefs;
             prefsModule.init(extension.metadata);
diff --git a/js/misc/extensionUtils.js b/js/misc/extensionUtils.js
index e49722409b..3e89a870d8 100644
--- a/js/misc/extensionUtils.js
+++ b/js/misc/extensionUtils.js
@@ -2,7 +2,7 @@
 /* exported ExtensionState, ExtensionType, getCurrentExtension,
    getSettings, initTranslations, gettext, ngettext, pgettext,
    openPrefs, isOutOfDate, installImporter, serializeExtension,
-   deserializeExtension */
+   deserializeExtension, setCurrentExtension */
 
 // Common utils for the extension system and the extension
 // preferences tool
@@ -13,6 +13,19 @@ const Gettext = imports.gettext;
 
 const Config = imports.misc.config;
 
+let Main = null;
+
+try {
+    Main = imports.ui.main;
+} catch (error) {
+    // Only log the error if it is not due to the
+    // missing import.
+    if (error?.name !== 'ImportError')
+        console.error(error);
+}
+
+let _extension = null;
+
 var ExtensionType = {
     SYSTEM: 1,
     PER_USER: 2,
@@ -41,6 +54,16 @@ const SERIALIZED_PROPERTIES = [
     'canChange',
 ];
 
+/**
+ * @param {object} extension the extension object to use in utilities like `initTranslations()`
+ */
+function setCurrentExtension(extension) {
+    if (Main)
+        throw new Error('setCurrentExtension() can only be called from outside the shell');
+
+    _extension = extension;
+}
+
 /**
  * getCurrentExtension:
  *
@@ -48,6 +71,9 @@ const SERIALIZED_PROPERTIES = [
  * an extension.
  */
 function getCurrentExtension() {
+    if (_extension)
+        return _extension;
+
     let stack = new Error().stack.split('\n');
     let extensionStackLine;
 


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