Bug 1041514 - Don't show default browser prompt if a user opts out in the installer. r?jimm draft
authorJared Wein <jwein@mozilla.com>
Wed, 17 Feb 2016 22:14:26 -0500
changeset 331721 6c80b56829bfe4af0c46135221300a41b8e7ffd4
parent 330176 cbd51059e3be43c0bf6d6cee6176fc547746cbae
child 514452 22ca0384e22af02ef8691ecce9e91d32011ca4f8
push id11062
push userjwein@mozilla.com
push dateThu, 18 Feb 2016 03:14:48 +0000
reviewersjimm
bugs1041514
milestone47.0a1
Bug 1041514 - Don't show default browser prompt if a user opts out in the installer. r?jimm MozReview-Commit-ID: JRIpOLXWmG1
browser/components/shell/ShellService.jsm
browser/installer/windows/nsis/installer.nsi
toolkit/modules/WindowsRegistry.jsm
--- a/browser/components/shell/ShellService.jsm
+++ b/browser/components/shell/ShellService.jsm
@@ -6,16 +6,18 @@
 
 this.EXPORTED_SYMBOLS = ["ShellService"];
 
 const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
 
 Cu.import("resource://gre/modules/AppConstants.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "WindowsRegistry",
+                                  "resource://gre/modules/WindowsRegistry.jsm");
 
 /**
  * Internal functionality to save and restore the docShell.allow* properties.
  */
 let ShellServiceInternal = {
   /**
    * Used to determine whether or not to offer "Set as desktop background"
    * functionality. Even if shell service is available it is not
@@ -49,21 +51,40 @@ let ShellServiceInternal = {
   _checkedThisSession: false,
   get shouldCheckDefaultBrowser() {
     // If we've already checked, the browser has been started and this is a
     // new window open, and we don't want to check again.
     if (this._checkedThisSession) {
       return false;
     }
 
-    return Services.prefs.getBoolPref("browser.shell.checkDefaultBrowser");
+    if (!Services.prefs.getBoolPref("browser.shell.checkDefaultBrowser")) {
+      return false;
+    }
+
+    if (AppConstants.platform == "win") {
+      let optOutValue = WindowsRegistry.readRegKey(Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
+                                                   "Software\\Mozilla\\Firefox",
+                                                   "DefaultBrowserOptOut");
+      WindowsRegistry.removeRegKey(Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
+                                   "Software\\Mozilla\\Firefox",
+                                   "DefaultBrowserOptOut");
+      if (optOutValue == "True") {
+        Services.prefs.setBoolPref("browser.shell.checkDefaultBrowser", false);
+        return false;
+      }
+    }
+
+    return true;
   },
+
   set shouldCheckDefaultBrowser(shouldCheck) {
     Services.prefs.setBoolPref("browser.shell.checkDefaultBrowser", !!shouldCheck);
   },
+
   isDefaultBrowser(startupCheck, forAllTypes) {
     // If this is the first browser window, maintain internal state that we've
     // checked this session (so that subsequent window opens don't show the
     // default browser dialog).
     if (startupCheck) {
       this._checkedThisSession = true;
     }
     if (this.shellService) {
--- a/browser/installer/windows/nsis/installer.nsi
+++ b/browser/installer/windows/nsis/installer.nsi
@@ -595,16 +595,22 @@ Section "-InstallEndCleanup"
       ${GetParameters} $0
       ${GetOptions} "$0" "/UAC:" $0
       ${If} ${Errors}
         Call SetAsDefaultAppUserHKCU
       ${Else}
         GetFunctionAddress $0 SetAsDefaultAppUserHKCU
         UAC::ExecCodeSegment $0
       ${EndIf}
+    ${Else}
+      ${LogHeader} "Writing default-browser opt-out"
+      WriteRegStr HKCU "Software\Mozilla\Firefox" "DefaultBrowserOptOut" "True"
+      ${If} ${Errors}
+        ${LogHeader} "Error writing default-browser opt-out"
+      ${EndIf}
     ${EndIf}
     ; Adds a pinned Task Bar shortcut (see MigrateTaskBarShortcut for details).
     ${MigrateTaskBarShortcut}
   ${EndUnless}
 
   ${GetShortcutsLogPath} $0
   WriteIniStr "$0" "TASKBAR" "Migrated" "true"
 
--- a/toolkit/modules/WindowsRegistry.jsm
+++ b/toolkit/modules/WindowsRegistry.jsm
@@ -42,9 +42,35 @@ var WindowsRegistry = {
         }
       }
     } catch (ex) {
     } finally {
       registry.close();
     }
     return undefined;
   },
+
+  /**
+   * Safely removes a key from the registry.
+   *
+   * @param aRoot
+   *        The root registry to use.
+   * @param aPath
+   *        The registry path to the key.
+   * @param aKey
+   *        The key name.
+   */
+  removeRegKey: function(aRoot, aPath, aKey) {
+    let registry = Cc["@mozilla.org/windows-registry-key;1"].
+                   createInstance(Ci.nsIWindowsRegKey);
+    try {
+      let mode = Ci.nsIWindowsRegKey.ACCESS_QUERY_VALUE |
+                 Ci.nsIWindowsRegKey.ACCESS_SET_VALUE;
+      registry.open(aRoot, aPath, mode);
+      if (registry.hasValue(aKey)) {
+        registry.removeValue(aKey);
+      }
+    } catch (ex) {
+    } finally {
+      registry.close();
+    }
+  }
 };