Bug 702748 - Use a pref for disabling per-site remembering of download directory; r=gavin.sharp, a=khuey

This commit is contained in:
Geoff Lankow 2011-12-12 17:31:50 +13:00
Родитель c00e0f1fef
Коммит 39535e4788
2 изменённых файлов: 48 добавлений и 3 удалений

Просмотреть файл

@ -50,9 +50,14 @@
* 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;
@ -109,13 +114,22 @@ function readLastDirPref() {
}
}
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"]
@ -133,8 +147,11 @@ let gDownloadLastDir = {
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)

Просмотреть файл

@ -224,9 +224,37 @@ function run_test() {
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");
}
}