2014-06-24 22:12:07 -07:00
|
|
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
|
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-20 22:54:41 -07:00
|
|
|
const nsIFile = Components.interfaces.nsIFile;
|
2009-08-25 02:22:43 -07:00
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.EXPORTED_SYMBOLS = [ "DownloadLastDir" ];
|
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-20 22:54:41 -07:00
|
|
|
Components.utils.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
|
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-20 22:54:41 -07:00
|
|
|
case "last-pb-context-exited":
|
|
|
|
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);
|
2012-06-30 07:50:07 -07:00
|
|
|
// Ensure that purging session history causes both the session-only PB cache
|
|
|
|
// and persistent prefs to be cleared.
|
2013-03-31 22:59:17 -07:00
|
|
|
let cps2 = Components.classes["@mozilla.org/content-pref/service;1"].
|
|
|
|
getService(Components.interfaces.nsIContentPrefService2);
|
|
|
|
|
|
|
|
cps2.removeByName(LAST_DIR_PREF, {usePrivateBrowsing: false});
|
|
|
|
cps2.removeByName(LAST_DIR_PREF, {usePrivateBrowsing: true});
|
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-20 22:54:41 -07:00
|
|
|
os.addObserver(observer, "last-pb-context-exited", 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-20 22:54:41 -07:00
|
|
|
return Services.prefs.getComplexValue(LAST_DIR_PREF, nsIFile);
|
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-20 22:54:41 -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-20 22:54:41 -07:00
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.DownloadLastDir = function DownloadLastDir(aWindow) {
|
2012-07-20 22:54:41 -07:00
|
|
|
this.window = aWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
DownloadLastDir.prototype = {
|
|
|
|
isPrivate: function DownloadLastDir_isPrivate() {
|
|
|
|
return PrivateBrowsingUtils.isWindowPrivate(this.window);
|
|
|
|
},
|
2011-06-09 16:19:16 -07:00
|
|
|
// compat shims
|
2013-08-08 20:38:49 -07:00
|
|
|
get file() this._getLastFile(),
|
2011-06-09 16:19:16 -07:00
|
|
|
set file(val) { this.setFile(null, val); },
|
2012-07-20 22:54:41 -07:00
|
|
|
cleanupPrivateFile: function () {
|
|
|
|
gDownloadLastDirFile = null;
|
|
|
|
},
|
2013-03-31 23:08:49 -07:00
|
|
|
// This function is now deprecated as it uses the sync nsIContentPrefService
|
|
|
|
// interface. New consumers should use the getFileAsync function.
|
2011-06-09 16:19:16 -07:00
|
|
|
getFile: function (aURI) {
|
2014-03-28 07:36:48 -07:00
|
|
|
let Deprecated = Components.utils.import("resource://gre/modules/Deprecated.jsm", {}).Deprecated;
|
2013-08-08 20:38:49 -07:00
|
|
|
Deprecated.warning("DownloadLastDir.getFile is deprecated. Please use getFileAsync instead.",
|
|
|
|
"https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/DownloadLastDir.jsm",
|
|
|
|
Components.stack.caller);
|
|
|
|
|
2011-12-11 20:31:50 -08:00
|
|
|
if (aURI && isContentPrefEnabled()) {
|
2012-06-30 07:50:07 -07:00
|
|
|
let loadContext = this.window
|
|
|
|
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Components.interfaces.nsIWebNavigation)
|
|
|
|
.QueryInterface(Components.interfaces.nsILoadContext);
|
|
|
|
let lastDir = Services.contentPrefs.getPref(aURI, LAST_DIR_PREF, loadContext);
|
2011-06-09 16:19:16 -07:00
|
|
|
if (lastDir) {
|
|
|
|
var lastDirFile = Components.classes["@mozilla.org/file/local;1"]
|
2012-07-20 22:54:41 -07:00
|
|
|
.createInstance(Components.interfaces.nsIFile);
|
2011-06-09 16:19:16 -07:00
|
|
|
lastDirFile.initWithPath(lastDir);
|
|
|
|
return lastDirFile;
|
|
|
|
}
|
|
|
|
}
|
2013-08-08 20:38:49 -07:00
|
|
|
return this._getLastFile();
|
|
|
|
},
|
|
|
|
|
|
|
|
_getLastFile: function () {
|
2009-03-28 00:50:36 -07:00
|
|
|
if (gDownloadLastDirFile && !gDownloadLastDirFile.exists())
|
|
|
|
gDownloadLastDirFile = null;
|
2009-01-13 10:00:05 -08:00
|
|
|
|
2012-07-20 22:54:41 -07:00
|
|
|
if (this.isPrivate()) {
|
|
|
|
if (!gDownloadLastDirFile)
|
|
|
|
gDownloadLastDirFile = readLastDirPref();
|
2009-08-25 02:22:43 -07:00
|
|
|
return gDownloadLastDirFile;
|
2012-07-20 22:54:41 -07:00
|
|
|
}
|
2013-08-08 20:38:49 -07:00
|
|
|
return readLastDirPref();
|
2009-01-13 10:00:05 -08:00
|
|
|
},
|
2013-03-31 23:08:49 -07:00
|
|
|
|
|
|
|
getFileAsync: function(aURI, aCallback) {
|
2013-08-08 20:38:49 -07:00
|
|
|
let plainPrefFile = this._getLastFile();
|
2013-03-31 23:08:49 -07:00
|
|
|
if (!aURI || !isContentPrefEnabled()) {
|
|
|
|
Services.tm.mainThread.dispatch(function() aCallback(plainPrefFile),
|
|
|
|
Components.interfaces.nsIThread.DISPATCH_NORMAL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let uri = aURI instanceof Components.interfaces.nsIURI ? aURI.spec : aURI;
|
|
|
|
let cps2 = Components.classes["@mozilla.org/content-pref/service;1"]
|
|
|
|
.getService(Components.interfaces.nsIContentPrefService2);
|
|
|
|
let loadContext = this.window
|
|
|
|
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Components.interfaces.nsIWebNavigation)
|
|
|
|
.QueryInterface(Components.interfaces.nsILoadContext);
|
|
|
|
let result = null;
|
|
|
|
cps2.getByDomainAndName(uri, LAST_DIR_PREF, loadContext, {
|
|
|
|
handleResult: function(aResult) result = aResult,
|
|
|
|
handleCompletion: function(aReason) {
|
|
|
|
let file = plainPrefFile;
|
|
|
|
if (aReason == Components.interfaces.nsIContentPrefCallback2.COMPLETE_OK &&
|
|
|
|
result instanceof Components.interfaces.nsIContentPref) {
|
|
|
|
file = Components.classes["@mozilla.org/file/local;1"]
|
|
|
|
.createInstance(Components.interfaces.nsIFile);
|
|
|
|
file.initWithPath(result.value);
|
|
|
|
}
|
|
|
|
aCallback(file);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2011-06-09 16:19:16 -07:00
|
|
|
setFile: function (aURI, aFile) {
|
2011-12-11 20:31:50 -08:00
|
|
|
if (aURI && isContentPrefEnabled()) {
|
2013-03-31 23:08:49 -07:00
|
|
|
let uri = aURI instanceof Components.interfaces.nsIURI ? aURI.spec : aURI;
|
|
|
|
let cps2 = Components.classes["@mozilla.org/content-pref/service;1"]
|
|
|
|
.getService(Components.interfaces.nsIContentPrefService2);
|
2012-06-30 07:50:07 -07:00
|
|
|
let loadContext = this.window
|
|
|
|
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Components.interfaces.nsIWebNavigation)
|
|
|
|
.QueryInterface(Components.interfaces.nsILoadContext);
|
2011-12-11 20:31:50 -08:00
|
|
|
if (aFile instanceof Components.interfaces.nsIFile)
|
2013-03-31 23:08:49 -07:00
|
|
|
cps2.set(uri, LAST_DIR_PREF, aFile.path, loadContext);
|
2011-12-11 20:31:50 -08:00
|
|
|
else
|
2013-03-31 23:08:49 -07:00
|
|
|
cps2.removeByDomainAndName(uri, LAST_DIR_PREF, loadContext);
|
2011-06-09 16:19:16 -07:00
|
|
|
}
|
2012-07-20 22:54:41 -07:00
|
|
|
if (this.isPrivate()) {
|
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-20 22:54:41 -07:00
|
|
|
Services.prefs.setComplexValue(LAST_DIR_PREF, nsIFile, 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
|
|
|
}
|
|
|
|
};
|