Bug 1595860 add a utility function to map pref names to extension settings r=robwu
authorShane Caraveo <scaraveo@mozilla.com>
Fri, 20 Dec 2019 18:14:36 +0000
changeset 508072 d81f3861a0f4980b97e2041c3b2129c2af285bde
parent 508071 db4fb3041de54092a6b1521b270f9020f7820612
child 508073 43f26061743cb8f64c3a853b7770892615b5dfb5
push id103786
push userscaraveo@mozilla.com
push dateFri, 20 Dec 2019 18:29:10 +0000
treeherderautoland@d81f3861a0f4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrobwu
bugs1595860
milestone73.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1595860 add a utility function to map pref names to extension settings r=robwu Differential Revision: https://phabricator.services.mozilla.com/D57909
toolkit/components/extensions/ExtensionPreferencesManager.jsm
toolkit/components/extensions/test/xpcshell/test_ext_extensionPreferencesManager.js
--- a/toolkit/components/extensions/ExtensionPreferencesManager.jsm
+++ b/toolkit/components/extensions/ExtensionPreferencesManager.jsm
@@ -230,16 +230,39 @@ this.ExtensionPreferencesManager = {
    *
    * @returns {string|number|boolean} The default value of the preference.
    */
   getDefaultValue(prefName) {
     return defaultPreferences.get(prefName);
   },
 
   /**
+   * Returns a map of prefName to setting Name for use in about:config, about:preferences or
+   * other areas of Firefox that need to know whether a specific pref is controlled by an
+   * extension.
+   *
+   * Given a prefName, you can get the settingName.  Call EPM.getSetting(settingName) to
+   * get the details of the setting, including which id if any is in control of the
+   * setting.
+   *
+   * @returns {Promise}
+   *          Resolves to a Map of prefName->settingName
+   */
+  async getManagedPrefDetails() {
+    await Management.asyncLoadSettingsModules();
+    let prefs = new Map();
+    settingsMap.forEach((setting, name) => {
+      for (let prefName of setting.prefNames) {
+        prefs.set(prefName, name);
+      }
+    });
+    return prefs;
+  },
+
+  /**
    * Indicates that an extension would like to change the value of a previously
    * defined setting.
    *
    * @param {string} id
    *        The id of the extension for which a setting is being set.
    * @param {string} name
    *        The unique id of the setting.
    * @param {any} value
--- a/toolkit/components/extensions/test/xpcshell/test_ext_extensionPreferencesManager.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_extensionPreferencesManager.js
@@ -864,8 +864,24 @@ add_task(async function test_preference_
   item = await ExtensionPreferencesManager.getSetting("some-pref");
   equal(item.value, "new value", "The value is extension set");
 
   await one.unload();
   await two.unload();
   await three.unload();
   await promiseShutdownManager();
 });
+
+add_task(async function test_preference_select() {
+  let prefNames = await ExtensionPreferencesManager.getManagedPrefDetails();
+  // Just check a subset of settings that are in this test file.
+  Assert.ok(prefNames.size > 0, "some prefs exist");
+  for (let settingName in SETTINGS) {
+    let setting = SETTINGS[settingName];
+    for (let prefName of setting.prefNames) {
+      Assert.equal(
+        prefNames.get(prefName),
+        settingName,
+        "setting retrieved prefNames"
+      );
+    }
+  }
+});