[gnome-shell] extensionUtils: Allow getCurrentExtension() to be called from anyone
- From: Jonh Wendell <jwendell src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-shell] extensionUtils: Allow getCurrentExtension() to be called from anyone
- Date: Tue, 11 Oct 2016 13:26:08 +0000 (UTC)
commit c405081d89245ad27aa01f31cebc4fe9bbc065b4
Author: Jonh Wendell <jonh wendell redhat com>
Date: Mon Oct 3 20:08:18 2016 -0300
extensionUtils: Allow getCurrentExtension() to be called from anyone
Currently it's assumed only an extension can call this method. However
it can be useful if any part of the shell want to know if it was invoked
by an extension.
https://bugzilla.gnome.org/show_bug.cgi?id=770717
js/misc/extensionUtils.js | 25 ++++++++++++++++++-------
1 files changed, 18 insertions(+), 7 deletions(-)
---
diff --git a/js/misc/extensionUtils.js b/js/misc/extensionUtils.js
index 1d1221c..aff91ac 100644
--- a/js/misc/extensionUtils.js
+++ b/js/misc/extensionUtils.js
@@ -21,14 +21,25 @@ const ExtensionType = {
// Maps uuid -> metadata object
const extensions = {};
+/**
+ * getCurrentExtension:
+ *
+ * Returns the current extension, or null if not called from an extension.
+ */
function getCurrentExtension() {
- let stack = (new Error()).stack;
-
- // Assuming we're importing this directly from an extension (and we shouldn't
- // ever not be), its UUID should be directly in the path here.
- let extensionStackLine = stack.split('\n')[1];
+ let stack = (new Error()).stack.split('\n');
+ let extensionStackLine;
+
+ // Search for an occurrence of an extension stack frame
+ // Start at 1 because 0 is the stack frame of this function
+ for (let i = 1; i < stack.length; i++) {
+ if (stack[i].indexOf('/gnome-shell/extensions/') > -1) {
+ extensionStackLine = stack[i];
+ break;
+ }
+ }
if (!extensionStackLine)
- throw new Error('Could not find current extension');
+ return null;
// The stack line is like:
// init([object Object])@/home/user/data/gnome-shell/extensions/u u id/prefs.js:8
@@ -38,7 +49,7 @@ function getCurrentExtension() {
// @/home/user/data/gnome-shell/extensions/u u id/prefs.js:8
let match = new RegExp('@(.+):\\d+').exec(extensionStackLine);
if (!match)
- throw new Error('Could not find current extension');
+ return null;
let path = match[1];
let file = Gio.File.new_for_path(path);
@@ -52,7 +63,7 @@ function getCurrentExtension() {
file = file.get_parent();
}
- throw new Error('Could not find current extension');
+ return null;
}
/**
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]