2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2009-01-13 10:00:05 -08:00
|
|
|
|
2009-11-23 16:24:13 -08:00
|
|
|
/*
|
|
|
|
* The behavior implemented by gDownloadLastDir is documented here.
|
|
|
|
*
|
|
|
|
* In normal browsing sessions, gDownloadLastDir uses the browser.download.lastDir
|
|
|
|
* preference to store the last used download directory. The first time the user
|
|
|
|
* switches into the private browsing mode, the last download directory is
|
|
|
|
* preserved to the pref value, but if the user switches to another directory
|
|
|
|
* 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.
|
2011-12-11 20:31:50 -08:00
|
|
|
*
|
|
|
|
* 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.
|
2009-11-23 16:24:13 -08:00
|
|
|
*/
|
|
|
|
|
2009-08-25 02:22:43 -07:00
|
|
|
const LAST_DIR_PREF = "browser.download.lastDir";
|
2011-12-11 20:31:50 -08:00
|
|
|
const SAVE_PER_SITE_PREF = LAST_DIR_PREF + ".savePerSite";
|
2012-07-21 11:29:50 -07:00
|
|
|
const PBSVC_CID = "@mozilla.org/privatebrowsing;1";
|
|
|
|
const nsILocalFile = Components.interfaces.nsILocalFile;
|
2009-08-25 02:22:43 -07:00
|
|
|
|
2012-07-21 11:29:50 -07:00
|
|
|
var EXPORTED_SYMBOLS = [ "gDownloadLastDir" ];
|
2009-01-13 10:00:05 -08:00
|
|
|
|
2011-06-09 16:19:16 -07:00
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
2012-07-21 11:29:50 -07:00
|
|
|
|
|
|
|
let pbSvc = null;
|
|
|
|
if (PBSVC_CID in Components.classes) {
|
|
|
|
pbSvc = Components.classes[PBSVC_CID]
|
|
|
|
.getService(Components.interfaces.nsIPrivateBrowsingService);
|
|
|
|
}
|
2009-08-25 02:22:43 -07:00
|
|
|
|
2009-01-13 10:00:05 -08:00
|
|
|
let observer = {
|
|
|
|
QueryInterface: function (aIID) {
|
|
|
|
if (aIID.equals(Components.interfaces.nsIObserver) ||
|
|
|
|
aIID.equals(Components.interfaces.nsISupports) ||
|
|
|
|
aIID.equals(Components.interfaces.nsISupportsWeakReference))
|
|
|
|
return this;
|
|
|
|
throw Components.results.NS_NOINTERFACE;
|
|
|
|
},
|
|
|
|
observe: function (aSubject, aTopic, aData) {
|
2009-11-16 17:14:28 -08:00
|
|
|
switch (aTopic) {
|
2012-07-21 11:29:50 -07:00
|
|
|
case "private-browsing":
|
|
|
|
if (aData == "enter")
|
|
|
|
gDownloadLastDirFile = readLastDirPref();
|
|
|
|
else if (aData == "exit") {
|
|
|
|
gDownloadLastDirFile = null;
|
|
|
|
}
|
2009-11-16 17:14:28 -08:00
|
|
|
break;
|
|
|
|
case "browser:purge-session-history":
|
|
|
|
gDownloadLastDirFile = null;
|
2011-06-09 16:19:16 -07:00
|
|
|
if (Services.prefs.prefHasUserValue(LAST_DIR_PREF))
|
|
|
|
Services.prefs.clearUserPref(LAST_DIR_PREF);
|
|
|
|
Services.contentPrefs.removePrefsByName(LAST_DIR_PREF);
|
2009-11-16 17:14:28 -08:00
|
|
|
break;
|
|
|
|
}
|
2009-01-13 10:00:05 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-11-16 17:14:28 -08:00
|
|
|
let os = Components.classes["@mozilla.org/observer-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIObserverService);
|
2012-07-21 11:29:50 -07:00
|
|
|
os.addObserver(observer, "private-browsing", true);
|
2009-11-16 17:14:28 -08:00
|
|
|
os.addObserver(observer, "browser:purge-session-history", true);
|
2009-01-13 10:00:05 -08:00
|
|
|
|
2009-08-25 02:22:43 -07:00
|
|
|
function readLastDirPref() {
|
|
|
|
try {
|
2012-07-21 11:29:50 -07:00
|
|
|
return Services.prefs.getComplexValue(LAST_DIR_PREF, nsILocalFile);
|
2009-08-25 02:22:43 -07:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-11 20:31:50 -08:00
|
|
|
function isContentPrefEnabled() {
|
|
|
|
try {
|
|
|
|
return Services.prefs.getBoolPref(SAVE_PER_SITE_PREF);
|
2012-07-21 11:29:50 -07:00
|
|
|
}
|
2011-12-11 20:31:50 -08:00
|
|
|
catch (e) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-25 02:22:43 -07:00
|
|
|
let gDownloadLastDirFile = readLastDirPref();
|
2012-07-21 11:29:50 -07:00
|
|
|
let gDownloadLastDir = {
|
2011-06-09 16:19:16 -07:00
|
|
|
// compat shims
|
|
|
|
get file() { return this.getFile(); },
|
|
|
|
set file(val) { this.setFile(null, val); },
|
|
|
|
getFile: function (aURI) {
|
2011-12-11 20:31:50 -08:00
|
|
|
if (aURI && isContentPrefEnabled()) {
|
2011-09-03 03:02:32 -07:00
|
|
|
let lastDir = Services.contentPrefs.getPref(aURI, LAST_DIR_PREF);
|
2011-06-09 16:19:16 -07:00
|
|
|
if (lastDir) {
|
|
|
|
var lastDirFile = Components.classes["@mozilla.org/file/local;1"]
|
2012-07-21 11:29:50 -07:00
|
|
|
.createInstance(Components.interfaces.nsILocalFile);
|
2011-06-09 16:19:16 -07:00
|
|
|
lastDirFile.initWithPath(lastDir);
|
|
|
|
return lastDirFile;
|
|
|
|
}
|
|
|
|
}
|
2009-03-28 00:50:36 -07:00
|
|
|
if (gDownloadLastDirFile && !gDownloadLastDirFile.exists())
|
|
|
|
gDownloadLastDirFile = null;
|
2009-01-13 10:00:05 -08:00
|
|
|
|
2012-07-21 11:29:50 -07:00
|
|
|
if (pbSvc && pbSvc.privateBrowsingEnabled)
|
2009-08-25 02:22:43 -07:00
|
|
|
return gDownloadLastDirFile;
|
|
|
|
else
|
|
|
|
return readLastDirPref();
|
2009-01-13 10:00:05 -08:00
|
|
|
},
|
2011-06-09 16:19:16 -07:00
|
|
|
setFile: function (aURI, aFile) {
|
2011-12-11 20:31:50 -08:00
|
|
|
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);
|
2011-06-09 16:19:16 -07:00
|
|
|
}
|
2012-07-21 11:29:50 -07:00
|
|
|
if (pbSvc && pbSvc.privateBrowsingEnabled) {
|
2011-06-09 16:19:16 -07:00
|
|
|
if (aFile instanceof Components.interfaces.nsIFile)
|
|
|
|
gDownloadLastDirFile = aFile.clone();
|
2009-08-25 02:22:43 -07:00
|
|
|
else
|
|
|
|
gDownloadLastDirFile = null;
|
|
|
|
} else {
|
2011-06-09 16:19:16 -07:00
|
|
|
if (aFile instanceof Components.interfaces.nsIFile)
|
2012-07-21 11:29:50 -07:00
|
|
|
Services.prefs.setComplexValue(LAST_DIR_PREF, nsILocalFile, aFile);
|
2011-06-09 16:19:16 -07:00
|
|
|
else if (Services.prefs.prefHasUserValue(LAST_DIR_PREF))
|
|
|
|
Services.prefs.clearUserPref(LAST_DIR_PREF);
|
2009-08-25 02:22:43 -07:00
|
|
|
}
|
2009-01-13 10:00:05 -08:00
|
|
|
}
|
|
|
|
};
|