Bug 1501649 - Add ability to delete a user-created pref on new "about:config" page. r=paolo
authorVincent Cote <vincent.cote@uleth.ca>
Thu, 22 Nov 2018 22:07:05 +0000
changeset 504224 1cdcda217f8b0e094c218e040fda6f334d6a1b65
parent 504177 8b245cc1086f912f84b54a6af13f015404af8e14
child 504225 f9d8c60591142bd751ff149f34b287d456df606c
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspaolo
bugs1501649
milestone65.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 1501649 - Add ability to delete a user-created pref on new "about:config" page. r=paolo Differential Revision: https://phabricator.services.mozilla.com/D11804
browser/components/aboutconfig/content/aboutconfig.js
browser/components/aboutconfig/content/aboutconfig.notftl
browser/components/aboutconfig/test/browser/browser.ini
browser/components/aboutconfig/test/browser/browser_edit.js
--- a/browser/components/aboutconfig/content/aboutconfig.js
+++ b/browser/components/aboutconfig/content/aboutconfig.js
@@ -1,23 +1,26 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 ChromeUtils.import("resource://gre/modules/Preferences.jsm");
 
+let gDefaultBranch = Services.prefs.getDefaultBranch("");
 let gPrefArray;
 
 function onLoad() {
   gPrefArray = Services.prefs.getChildList("").map(function(name) {
+    let hasUserValue = Services.prefs.prefHasUserValue(name);
     let pref = {
       name,
       value: Preferences.get(name),
-      hasUserValue: Services.prefs.prefHasUserValue(name),
+      hasUserValue,
+      hasDefaultValue: hasUserValue ? prefHasDefaultValue(name) : true,
     };
     // Try in case it's a localized string.
     // Throws an exception if there is no equivalent value in the localized files for the pref.
     // If an execption is thrown the pref value is set to the empty string.
     try {
       if (!pref.hasUserValue && /^chrome:\/\/.+\/locale\/.+\.properties/.test(pref.value)) {
         pref.value = Services.prefs.getComplexValue(name, Ci.nsIPrefLocalizedString).data;
       }
@@ -31,16 +34,26 @@ function onLoad() {
 
   document.getElementById("search").addEventListener("keypress", function(e) {
     if (e.code == "Enter") {
       filterPrefs();
     }
   });
 
   document.getElementById("prefs").appendChild(createPrefsFragment(gPrefArray));
+  document.getElementById("prefs").addEventListener("click", (event) => {
+    if (event.target.localName != "button") {
+      return;
+    }
+    let prefRow = event.target.closest("tr");
+    let prefName = prefRow.getAttribute("aria-label");
+    Services.prefs.clearUserPref(prefName);
+    gPrefArray.splice(gPrefArray.findIndex(pref => pref.name == prefName), 1);
+    prefRow.remove();
+  });
 }
 
 function filterPrefs() {
   let substring = document.getElementById("search").value.trim();
   let fragment = createPrefsFragment(gPrefArray.filter(pref => pref.name.includes(substring)));
   document.getElementById("prefs").textContent = "";
   document.getElementById("prefs").appendChild(fragment);
 }
@@ -63,12 +76,37 @@ function createPrefsFragment(prefArray) 
     nameCell.append(parts[parts.length - 1]);
     row.appendChild(nameCell);
 
     let valueCell = document.createElement("td");
     valueCell.classList.add("cell-value");
     valueCell.textContent = pref.value;
     row.appendChild(valueCell);
 
+    let buttonCell = document.createElement("td");
+    if (!pref.hasDefaultValue) {
+      let button = document.createElement("button");
+      document.l10n.setAttributes(button, "about-config-pref-delete");
+      buttonCell.appendChild(button);
+    }
+    row.appendChild(buttonCell);
+
     fragment.appendChild(row);
   }
   return fragment;
 }
+
+function prefHasDefaultValue(name) {
+  try {
+    switch (Services.prefs.getPrefType(name)) {
+      case Ci.nsIPrefBranch.PREF_STRING:
+        gDefaultBranch.getStringPref(name);
+        return true;
+      case Ci.nsIPrefBranch.PREF_INT:
+        gDefaultBranch.getIntPref(name);
+        return true;
+      case Ci.nsIPrefBranch.PREF_BOOL:
+        gDefaultBranch.getBoolPref(name);
+        return true;
+    }
+  } catch (ex) {}
+  return false;
+}
--- a/browser/components/aboutconfig/content/aboutconfig.notftl
+++ b/browser/components/aboutconfig/content/aboutconfig.notftl
@@ -1,8 +1,10 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 about-config-title = about:config
 
+about-config-pref-delete = Delete
+
 about-config-search =
     .placeholder = Search
--- a/browser/components/aboutconfig/test/browser/browser.ini
+++ b/browser/components/aboutconfig/test/browser/browser.ini
@@ -1,5 +1,6 @@
 [DEFAULT]
 
 [browser_basic.js]
+[browser_edit.js]
 [browser_search.js]
 skip-if = debug # Bug 1507747
new file mode 100644
--- /dev/null
+++ b/browser/components/aboutconfig/test/browser/browser_edit.js
@@ -0,0 +1,26 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+const PAGE_URL = "chrome://browser/content/aboutconfig/aboutconfig.html";
+
+add_task(async function test_delete_user_pref() {
+  Services.prefs.setBoolPref("userAddedPref", true);
+  await BrowserTestUtils.withNewTab({
+    gBrowser,
+    url: PAGE_URL,
+  }, browser => {
+    return ContentTask.spawn(browser, null, () => {
+      let list = [...content.document.getElementById("prefs")
+        .getElementsByTagName("tr")];
+      function getRow(name) {
+        return list.find(row => row.querySelector("td").textContent == name);
+      }
+      Assert.ok(getRow("userAddedPref"));
+      getRow("userAddedPref").lastChild.lastChild.click();
+      list = [...content.document.getElementById("prefs")
+        .getElementsByTagName("tr")];
+      Assert.ok(!getRow("userAddedPref"));
+      Assert.ok(!Services.prefs.getChildList("").includes("userAddedPref"));
+    });
+  });
+});