Bug 1458308 - Change about:preferences to use the new auto update pref mechanism draft
authorKirk Steuber <ksteuber@mozilla.com>
Mon, 09 Jul 2018 11:58:03 -0700
changeset 829192 dde7f7c1dd4d09f3c306e0d2220af37a22b621e1
parent 829191 bbd4561e14d30b21f81fc286e78f951dd1e06755
child 829193 c1445cf55db73e6fc916cc4a639168ede1197d13
push id118747
push userbmo:ksteuber@mozilla.com
push dateTue, 14 Aug 2018 21:08:44 +0000
bugs1458308
milestone63.0a1
Bug 1458308 - Change about:preferences to use the new auto update pref mechanism MozReview-Commit-ID: HzjUl2nu2OJ
browser/components/preferences/in-content/main.js
browser/components/preferences/in-content/main.xul
browser/locales/en-US/browser/preferences/preferences.ftl
--- a/browser/components/preferences/in-content/main.js
+++ b/browser/components/preferences/in-content/main.js
@@ -15,16 +15,17 @@ ChromeUtils.import("resource:///modules/
 ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
 ChromeUtils.import("resource://gre/modules/DownloadUtils.jsm");
 ChromeUtils.import("resource://gre/modules/L10nRegistry.jsm");
 ChromeUtils.import("resource://gre/modules/Localization.jsm");
 ChromeUtils.defineModuleGetter(this, "CloudStorage",
   "resource://gre/modules/CloudStorage.jsm");
 
 XPCOMUtils.defineLazyServiceGetters(this, {
+  gAUS: ["@mozilla.org/updates/update-service;1", "nsIApplicationUpdateService"],
   gCategoryManager: ["@mozilla.org/categorymanager;1", "nsICategoryManager"],
   gHandlerService: ["@mozilla.org/uriloader/handler-service;1", "nsIHandlerService"],
   gMIMEService: ["@mozilla.org/mime;1", "nsIMIMEService"],
 });
 
 // Constants & Enumeration Values
 const TYPE_MAYBE_FEED = "application/vnd.mozilla.maybe.feed";
 const TYPE_MAYBE_VIDEO_FEED = "application/vnd.mozilla.maybe.video.feed";
@@ -74,16 +75,18 @@ const PREF_FEED_SELECTED_READER = "brows
 const PREF_VIDEO_FEED_SELECTED_APP = "browser.videoFeeds.handlers.application";
 const PREF_VIDEO_FEED_SELECTED_ACTION = "browser.videoFeeds.handler";
 const PREF_VIDEO_FEED_SELECTED_READER = "browser.videoFeeds.handler.default";
 
 const PREF_AUDIO_FEED_SELECTED_APP = "browser.audioFeeds.handlers.application";
 const PREF_AUDIO_FEED_SELECTED_ACTION = "browser.audioFeeds.handler";
 const PREF_AUDIO_FEED_SELECTED_READER = "browser.audioFeeds.handler.default";
 
+const AUTO_UPDATE_CHANGED_TOPIC = "auto-update-pref-change";
+
 // The nsHandlerInfoAction enumeration values in nsIHandlerInfo identify
 // the actions the application can take with content of various types.
 // But since nsIHandlerInfo doesn't support plugins, there's no value
 // identifying the "use plugin" action, so we use this constant instead.
 const kActionUsePlugin = 5;
 
 const ICON_URL_APP = AppConstants.platform == "linux" ?
   "moz-icon://dummy.exe?size=16" :
@@ -215,17 +218,16 @@ if (AppConstants.platform === "win") {
   Preferences.addAll([
     { id: "browser.taskbar.previews.enable", type: "bool" },
     { id: "ui.osk.enabled", type: "bool" },
   ]);
 }
 
 if (AppConstants.MOZ_UPDATER) {
   Preferences.addAll([
-    { id: "app.update.auto", type: "bool" },
     { id: "app.update.disable_button.showUpdateHistory", type: "bool" },
   ]);
 
   if (AppConstants.MOZ_MAINTENANCE_SERVICE) {
     Preferences.addAll([
       { id: "app.update.service.enabled", type: "bool" },
     ]);
   }
@@ -517,16 +519,24 @@ var gMainPane = {
         gMainPane.showUpdates);
 
       if (Services.policies && !Services.policies.isAllowed("appUpdate")) {
         document.getElementById("updateAllowDescription").hidden = true;
         document.getElementById("updateRadioGroup").hidden = true;
         if (AppConstants.MOZ_MAINTENANCE_SERVICE) {
           document.getElementById("useService").hidden = true;
         }
+      } else {
+        // Start with no option selected since we are still reading the value
+        document.getElementById("autoDesktop").removeAttribute("selected");
+        document.getElementById("manualDesktop").removeAttribute("selected");
+        // Start reading the correct value from the disk
+        this.updateReadPrefs();
+        setEventListener("updateRadioGroup", "command",
+                         gMainPane.updateWritePrefs);
       }
 
       if (AppConstants.MOZ_MAINTENANCE_SERVICE) {
         // Check to see if the maintenance service is installed.
         // If it isn't installed, don't show the preference at all.
         let installed;
         try {
           let wrk = Cc["@mozilla.org/windows-registry-key;1"]
@@ -556,16 +566,17 @@ var gMainPane = {
 
     Services.prefs.addObserver(PREF_VIDEO_FEED_SELECTED_APP, this);
     Services.prefs.addObserver(PREF_VIDEO_FEED_SELECTED_ACTION, this);
     Services.prefs.addObserver(PREF_VIDEO_FEED_SELECTED_READER, this);
 
     Services.prefs.addObserver(PREF_AUDIO_FEED_SELECTED_APP, this);
     Services.prefs.addObserver(PREF_AUDIO_FEED_SELECTED_ACTION, this);
     Services.prefs.addObserver(PREF_AUDIO_FEED_SELECTED_READER, this);
+    Services.obs.addObserver(this, AUTO_UPDATE_CHANGED_TOPIC);
 
     setEventListener("filter", "command", gMainPane.filter);
     setEventListener("typeColumn", "click", gMainPane.sort);
     setEventListener("actionColumn", "click", gMainPane.sort);
     setEventListener("chooseFolder", "command", gMainPane.chooseFolder);
     setEventListener("saveWhere", "command", gMainPane.handleSaveToCommand);
     Preferences.get("browser.download.folderList").on("change",
       gMainPane.displayDownloadDirPref.bind(gMainPane));
@@ -1251,16 +1262,67 @@ var gMainPane = {
       document.getElementById("limitContentProcess").disabled = true;
       document.getElementById("contentProcessCount").disabled = true;
       document.getElementById("contentProcessCountEnabledDescription").hidden = true;
       document.getElementById("contentProcessCountDisabledDescription").hidden = false;
     }
   },
 
   /**
+   * Selects the correct item in the update radio group
+   */
+  async updateReadPrefs() {
+    if (AppConstants.MOZ_UPDATER &&
+        (!Services.policies || Services.policies.isAllowed("appUpdate"))) {
+      let radiogroup = document.getElementById("updateRadioGroup");
+      radiogroup.disabled = true;
+      try {
+        let enabled = await gAUS.autoUpdateIsEnabled();
+        radiogroup.value = enabled;
+        radiogroup.disabled = false;
+      } catch (error) {
+        Cu.reportError(error);
+      }
+    }
+  },
+
+  /**
+   * Writes the value of the update radio group to the disk
+   */
+  async updateWritePrefs() {
+    if (AppConstants.MOZ_UPDATER &&
+        (!Services.policies || Services.policies.isAllowed("appUpdate"))) {
+      let radiogroup = document.getElementById("updateRadioGroup");
+      let updateAutoValue = (radiogroup.value == "true");
+      radiogroup.disabled = true;
+      try {
+        await gAUS.setAutoUpdateEnabled(updateAutoValue);
+        radiogroup.disabled = false;
+      } catch (error) {
+        Cu.reportError(error);
+        await this.updateReadPrefs();
+        await this.reportUpdatePrefWriteError(error);
+      }
+    }
+  },
+
+  async reportUpdatePrefWriteError(error) {
+    let [title, message] = await document.l10n.formatValues([
+      {id: "update-pref-write-failure-title"},
+      {id: "update-pref-write-failure-message", args: {path: error.path}},
+    ]);
+
+    // Set up the Ok Button
+    let buttonFlags = (Services.prompt.BUTTON_POS_0 *
+                       Services.prompt.BUTTON_TITLE_OK);
+    Services.prompt.confirmEx(window, title, message, buttonFlags,
+                              null, null, null, null, {});
+  },
+
+  /**
    * Displays the history of installed updates.
    */
   showUpdates() {
     gSubDialog.open("chrome://mozapps/content/update/history.xul");
   },
 
   destroy() {
     window.removeEventListener("unload", this);
@@ -1274,16 +1336,18 @@ var gMainPane = {
     Services.prefs.removeObserver(PREF_VIDEO_FEED_SELECTED_ACTION, this);
     Services.prefs.removeObserver(PREF_VIDEO_FEED_SELECTED_READER, this);
 
     Services.prefs.removeObserver(PREF_AUDIO_FEED_SELECTED_APP, this);
     Services.prefs.removeObserver(PREF_AUDIO_FEED_SELECTED_ACTION, this);
     Services.prefs.removeObserver(PREF_AUDIO_FEED_SELECTED_READER, this);
 
     Services.prefs.removeObserver(PREF_CONTAINERS_EXTENSION, this);
+
+    Services.obs.removeObserver(this, AUTO_UPDATE_CHANGED_TOPIC);
   },
 
 
   // nsISupports
 
   QueryInterface: ChromeUtils.generateQI([Ci.nsIObserver]),
 
   // nsIObserver
@@ -1304,16 +1368,18 @@ var gMainPane = {
           await this._rebuildVisibleTypes();
           this._sortVisibleTypes();
         }
 
         // All the prefs we observe can affect what we display, so we rebuild
         // the view when any of them changes.
         this._rebuildView();
       }
+    } else if (aTopic == AUTO_UPDATE_CHANGED_TOPIC) {
+      document.getElementById("updateRadioGroup").value = aData;
     }
   },
 
 
   // EventListener
 
   handleEvent(aEvent) {
     if (aEvent.type == "unload") {
--- a/browser/components/preferences/in-content/main.xul
+++ b/browser/components/preferences/in-content/main.xul
@@ -548,21 +548,22 @@
                 disabled="true"/>
       </hbox>
     </deck>
   </vbox>
 #endif
 
 #ifdef MOZ_UPDATER
   <description id="updateAllowDescription" data-l10n-id="update-application-allow-description"></description>
-  <radiogroup id="updateRadioGroup" preference="app.update.auto">
+  <radiogroup id="updateRadioGroup">
     <radio id="autoDesktop"
            value="true"
            data-l10n-id="update-application-auto"/>
-    <radio value="false"
+    <radio id="manualDesktop"
+           value="false"
            data-l10n-id="update-application-check-choose"/>
   </radiogroup>
 #ifdef MOZ_MAINTENANCE_SERVICE
   <checkbox id="useService"
             data-l10n-id="update-application-use-service"
             preference="app.update.service.enabled"/>
 #endif
 #endif
--- a/browser/locales/en-US/browser/preferences/preferences.ftl
+++ b/browser/locales/en-US/browser/preferences/preferences.ftl
@@ -347,16 +347,20 @@ update-application-manual =
 update-application-use-service =
     .label = Use a background service to install updates
     .accesskey = b
 
 update-enable-search-update =
     .label = Automatically update search engines
     .accesskey = e
 
+update-pref-write-failure-title = Write Failure
+
+update-pref-write-failure-message = Unable to save preference. Could not write to file: { $path }
+
 ## General Section - Performance
 
 performance-title = Performance
 
 performance-use-recommended-settings-checkbox =
     .label = Use recommended performance settings
     .accesskey = U