Bug 702748 - Use a pref for disabling per-site remembering of download directory; r=gavin.sharp, a=khuey
authorGeoff Lankow <geoff@darktrojan.net>
Mon, 12 Dec 2011 17:31:50 +1300
changeset 84068 1e81f179adf27dafe7d0ae3fe4688a14f2cdf6b8
parent 84067 72a93b3fa2f95769aba4a427c0f3f4963f06b828
child 84069 5c64fb241d4ea51d4dd01919461c6c7ce97b576d
child 84632 502049cfb331e7677454be8f2627c51f50708839
push id519
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 00:38:35 +0000
treeherdermozilla-beta@788ea1ef610b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgavin, khuey
bugs702748
milestone11.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 702748 - Use a pref for disabling per-site remembering of download directory; r=gavin.sharp, a=khuey
toolkit/mozapps/downloads/DownloadLastDir.jsm
toolkit/mozapps/downloads/tests/unit/test_DownloadLastDirWithCPS.js
--- a/toolkit/mozapps/downloads/DownloadLastDir.jsm
+++ b/toolkit/mozapps/downloads/DownloadLastDir.jsm
@@ -45,19 +45,24 @@
  * during the private browsing mode, that directory is not stored in the pref,
  * and will be merely kept in memory.  When leaving the private browsing mode,
  * this in-memory value will be discarded, and the last download directory
  * will be reverted to the pref value.
  *
  * Both the pref and the in-memory value will be cleared when clearing the
  * browsing history.  This effectively changes the last download directory
  * to the default download directory on each platform.
+ *
+ * If passed a URI, the last used directory is also stored with that URI in the
+ * content preferences database. This can be disabled by setting the pref
+ * browser.download.lastDir.savePerSite to false.
  */
 
 const LAST_DIR_PREF = "browser.download.lastDir";
+const SAVE_PER_SITE_PREF = LAST_DIR_PREF + ".savePerSite";
 const PBSVC_CID = "@mozilla.org/privatebrowsing;1";
 const nsILocalFile = Components.interfaces.nsILocalFile;
 
 var EXPORTED_SYMBOLS = [ "gDownloadLastDir" ];
 
 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
 Components.utils.import("resource://gre/modules/Services.jsm");
 Components.utils.import("resource://gre/modules/Dict.jsm");
@@ -104,23 +109,32 @@ function readLastDirPref() {
   try {
     return Services.prefs.getComplexValue(LAST_DIR_PREF, nsILocalFile);
   }
   catch (e) {
     return null;
   }
 }
 
+function isContentPrefEnabled() {
+  try {
+    return Services.prefs.getBoolPref(SAVE_PER_SITE_PREF);
+  } 
+  catch (e) {
+    return true;
+  }
+}
+
 let gDownloadLastDirFile = readLastDirPref();
 let gDownloadLastDir = {
   // compat shims
   get file() { return this.getFile(); },
   set file(val) { this.setFile(null, val); },
   getFile: function (aURI) {
-    if (aURI) {
+    if (aURI && isContentPrefEnabled()) {
       let lastDir = Services.contentPrefs.getPref(aURI, LAST_DIR_PREF);
       if (lastDir) {
         var lastDirFile = Components.classes["@mozilla.org/file/local;1"]
                                     .createInstance(Components.interfaces.nsILocalFile);
         lastDirFile.initWithPath(lastDir);
         return lastDirFile;
       }
     }
@@ -128,18 +142,21 @@ let gDownloadLastDir = {
       gDownloadLastDirFile = null;
 
     if (pbSvc && pbSvc.privateBrowsingEnabled)
       return gDownloadLastDirFile;
     else
       return readLastDirPref();
   },
   setFile: function (aURI, aFile) {
-    if (aURI) {
-      Services.contentPrefs.setPref(aURI, LAST_DIR_PREF, aFile.path);
+    if (aURI && isContentPrefEnabled()) {
+      if (aFile instanceof Components.interfaces.nsIFile)
+        Services.contentPrefs.setPref(aURI, LAST_DIR_PREF, aFile.path);
+      else
+        Services.contentPrefs.removePref(aURI, LAST_DIR_PREF);
     }
     if (pbSvc && pbSvc.privateBrowsingEnabled) {
       if (aFile instanceof Components.interfaces.nsIFile)
         gDownloadLastDirFile = aFile.clone();
       else
         gDownloadLastDirFile = null;
     } else {
       if (aFile instanceof Components.interfaces.nsIFile)
--- a/toolkit/mozapps/downloads/tests/unit/test_DownloadLastDirWithCPS.js
+++ b/toolkit/mozapps/downloads/tests/unit/test_DownloadLastDirWithCPS.js
@@ -219,14 +219,42 @@ function run_test() {
       clearHistory();
       do_check_eq(gDownloadLastDir.file, null);
       do_check_eq(gDownloadLastDir.getFile(uri1), null);
 
       pb.privateBrowsingEnabled = false;
       do_check_eq(gDownloadLastDir.file, null);
       do_check_eq(gDownloadLastDir.getFile(uri1), null);
     }
+
+    { // check that disabling CPS works
+      Services.prefs.setBoolPref("browser.download.lastDir.savePerSite", false);
+
+      gDownloadLastDir.setFile(uri1, dir1);
+      do_check_eq(gDownloadLastDir.file.path, dir1.path);
+      do_check_eq(gDownloadLastDir.getFile(uri1).path, dir1.path);
+      do_check_eq(gDownloadLastDir.getFile(uri2).path, dir1.path);
+      do_check_eq(gDownloadLastDir.getFile(uri3).path, dir1.path);
+      do_check_eq(gDownloadLastDir.getFile(uri4).path, dir1.path);
+
+      gDownloadLastDir.setFile(uri2, dir2);
+      do_check_eq(gDownloadLastDir.file.path, dir2.path);
+      do_check_eq(gDownloadLastDir.getFile(uri1).path, dir2.path);
+      do_check_eq(gDownloadLastDir.getFile(uri2).path, dir2.path);
+      do_check_eq(gDownloadLastDir.getFile(uri3).path, dir2.path);
+      do_check_eq(gDownloadLastDir.getFile(uri4).path, dir2.path);
+
+      Services.prefs.clearUserPref("browser.download.lastDir.savePerSite");
+    }
+
+    { // check that passing null to setFile clears the stored value
+      gDownloadLastDir.setFile(uri3, dir3);
+      do_check_eq(gDownloadLastDir.getFile(uri3).path, dir3.path);
+      gDownloadLastDir.setFile(uri3, null);
+      do_check_eq(gDownloadLastDir.getFile(uri3), null);
+    }
   } finally {
     dir1.remove(true);
     dir2.remove(true);
     dir3.remove(true);
+    Services.prefs.clearUserPref("browser.download.lastDir.savePerSite");
   }
 }